3 回答

慕婉清6462132
TA貢獻1804條經驗 獲得超2個贊
exec返回具有index屬性的對象:
var match = /bar/.exec("foobar");
if (match) {
console.log("match found at " + match.index);
}
對于多個匹配項:
var re = /bar/g,
str = "foobarfoobar";
while ((match = re.exec(str)) != null) {
console.log("match found at " + match.index);
}

手掌心
TA貢獻1942條經驗 獲得超3個贊
這是我想出的:
// Finds starting and ending positions of quoted text
// in double or single quotes with escape char support like \" \'
var str = "this is a \"quoted\" string as you can 'read'";
var patt = /'((?:\\.|[^'])*)'|"((?:\\.|[^"])*)"/igm;
while (match = patt.exec(str)) {
console.log(match.index + ' ' + patt.lastIndex);
}
添加回答
舉報
0/150
提交
取消