我有一个看起来像这样的字符串:
{{tagName(21, 'hello, jane','smith')}}
我正在尝试使用正则表达式来 match()
这个字符串以得到:
[0] = tagName
[1] = 21
[2] = 'hello, jane'
[3] = 'smith'
字符串的参数部分可以增长。也就是说,它可能有更多或更少的参数,正则表达式需要“贪婪”但知道如何将它们分组。
我一直在尝试类似的方法:^\{\{([^\(]+)\({1}(.*)\){1}\}\}
但这会导致:
[0] = tagName
[1] = 21, 'hello, jane','smith'
我应该如何处理我的正则表达式以获得我想要的结果?
最佳答案
将{
, }
, (
, )
替换为空字符串;匹配 [a-z]+
、\d+
、'.+'
后跟 、
或输入结束
var str = "{{tagName(21, 'hello, jane','smith')}}";
var res = str.replace(/\{|\}|\(|\)/g, "")
.match(/([a-z]+)|\d+|('.+')(?=,)|('.+')(?=$)/ig);
console.log(res);
关于javascript - 如何使用正则表达式参数化以逗号分隔的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39239888/