Javascript RegExp匹配&;多个反向引用
到目前为止,我在尝试在javascript匹配中使用多个反向引用时遇到问题:-Javascript RegExp匹配&;多个反向引用,javascript,jquery,regex,replace,match,Javascript,Jquery,Regex,Replace,Match,到目前为止,我在尝试在javascript匹配中使用多个反向引用时遇到问题:- function newIlluminate() { var string = "the time is a quarter to two"; var param = "time"; var re = new RegExp("(" + param + ")", "i"); var test = new RegExp("(time)(quarter)(the)", "i"); var m
function newIlluminate() {
var string = "the time is a quarter to two";
var param = "time";
var re = new RegExp("(" + param + ")", "i");
var test = new RegExp("(time)(quarter)(the)", "i");
var matches = string.match(test);
$("#debug").text(matches[1]);
}
newIlluminate();
#调试当匹配Regex“re”打印“time”时,它是param的值
我见过匹配示例,其中使用多个反向引用将匹配包装在括号中,但我的匹配(时间)(季度)。。。正在返回null
我哪里做错了?任何帮助都将不胜感激 您的正则表达式实际上是在查找
timequarter
并将匹配项(如果找到)拆分为三个反向引用
我想你的意思是:
var test = /time|quarter|the/ig;
您的regex
测试
与字符串
根本不匹配(因为它不包含子字符串timequartthe
)。我想你想要:
您的regex
测试
与字符串
根本不匹配(因为它不包含子字符串timequartthe
)。您想要什么?期望的输出是什么?理想情况下,我希望循环字符串中的匹配项,并将每个匹配项包装在一个范围内。所以我希望比赛[1]=时间,比赛[2]=四分之一等等,因为这是第一场比赛g
更改行为,使其按照找到匹配项的顺序返回所有匹配项。是的,我发现在我发表评论几秒钟后,我的头在这太长的时间里哪儿也去不了了,谢谢!标记为答案,因为它是第一个:)
var test = /time|quarter|the/ig; // does not even need a capturing group
var matches = string.match(test);
$("#debug").text(matches!=null ? matches.join(", ") : "did not match");