该提案提供了一个新的
/d
,用来获取每个匹配的开始位置和结束位置信息。
let str = 'The question is TO BE, or not to be, that is to be. is to do.',
regex = /to/gd,
arr = [...str.matchAll(regex)].reduce((pre, cur) => (pre.concat([cur.indices[0]])), []);
for (let i = 0; i < str.length; i++) if (arr.toString().split(',').includes(String(i))) console.log(str[i]);