小编典典

返回 Javascript 中正则表达式 match() 的位置?

all

有没有办法在 Javascript 中检索正则表达式 match() 结果的字符串中的(起始)字符位置?


阅读 55

收藏
2022-06-27

共1个答案

小编典典

这是我想出的:

// 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);

}
2022-06-27