到目前为止,我在尝试在 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 matches = string.match(test);
$("#debug").text(matches[1]);
}
newIlluminate();
#Debug 当匹配正则表达式“re”时,会打印“time”,即 param 的值。
我见过匹配示例,其中通过将匹配括在括号中来使用多个反向引用,但是我对(时间)(季度)...的匹配返回 null。
我哪里出错了?任何帮助将不胜感激!
最佳答案
您的正则表达式实际上是在查找 timequarterthe
并将匹配项(如果找到)拆分为三个反向引用。
我认为你的意思是:
var test = /time|quarter|the/ig;
关于Javascript正则表达式匹配和多个反向引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18289296/