3 回答

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

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
這是我想出的:
// 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);
}
添加回答
舉報(bào)