javascript 正则分组捕捉
1 使用match 不能加/g 否则无效
2 使用matchAll 需要先定义一个RegExp对象
const regexp = RegExp(/,"(.+)"\)/,'g'); const matches = str.matchAll(regexp); for (const match of matches) { console.log(`Found ${match[0]} start=${match.index} end=${match.index + match[0].length}.`); console.log(match[1]); }
可以使用exec
const regexp = RegExp('foo[a-z]*','g');
const str = 'table football, foosball';
let match;
while ((match = regexp.exec(str)) !== null) {
console.log(`Found ${match[0]} start=${match.index} end=${regexp.lastIndex}.`);
// expected output: "Found football start=6 end=14."
// expected output: "Found foosball start=16 end=24."
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll

浙公网安备 33010602011771号