可以从regex对象获取组项列表吗? 例如,我有这个正则表达式:
var someRegex = /hello\s(.*?), i have (\d+) friends! (?:Bye)/;
我希望能够得到:
['(.*?)', '(\\d+)'];
(请注意,我不想获得' Bye'组,因为它有'?:'标记..)
谢谢!
答案 0 :(得分:1)
\((?!\?:)[^(]*\)
试试这个。看看演示。
https://regex101.com/r/bN8dL3/7
var re = /\((?!\?:)[^(]*\)/gi;
var str = 'hello\s(.*?), i have (\d+) friends! (?:Bye)';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}