一尘不染

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

javascript

有没有办法在Java中的regex match()结果的字符串中检索(开始)字符位置?


阅读 554

收藏
2020-05-01

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

}
2020-05-01