给定一个由冒号分隔的字符串,类似于此...
xvf:metric:admin:click
我需要捕获三个组...
xvf
metric
admin:click
或者另一个例子:
one:two:three:four:five:six:seven
one
two
three:four:five:six:seven
我当前的正则表达式只是分别捕获每个单词,导致 4 个匹配
/(\s*\w+)/gi
最佳答案
使用String.match
和Array.slice
函数的解决方案:
var str = "one:two:three:four:five:six:seven",
groups = str.match(/([^:]+?):([^:]+?):(.+)?$/).slice(1);
console.log(groups); // ["one", "two", "three:four:five:six:seven"]
关于javascript - 使用正则表达式捕获组一定次数,但最后一组有剩余值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37736195/