在Javascript中返回正则表达式匹配()的位置? [英] Return positions of a regex match() in Javascript?

查看:159
本文介绍了在Javascript中返回正则表达式匹配()的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to retrieve the (starting) character positions inside a string of the results of a regex match() in Javascript?

推荐答案

以下是我的想法:

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

这篇关于在Javascript中返回正则表达式匹配()的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆