我的模式在第一次匹配后停止,并且在那里有全局匹配。
//When I add [*]* like var pattern=/([^A-z0-9]|^|[*]*)_(\S.*?)_((?!\S)|\W)/g;
// it works, but when I try to match "1_test1_" and "a_test1_" it matches "_test1_"
// which I don't want. I know [*]* will match 0 or more instances of literal *
// but [*]+ won't work due to the first match being "_test1_*"
var pattern=/([^A-z0-9]|^)_(\S.*?)_((?!\S)|\W)/g;
alert("_test1_*_test2_".match(pattern)); //=> _test1_*
//This should match "_test1_*" first then it should also add to the array with "_test2_"
问题开始
我希望上面的(第一个代码块)提醒“_test1_*,_test2_”,我希望下面的(第二个代码块)保持不变(如注释部分所示)。
我不知道为什么 _test2_ 不匹配,因为它与下面显示的测试完全匹配。
问题完成
以下是应有的测试和工作。
alert("_test1_ _test2_".match(pattern)); //=> _test1_, _test2_
alert("_test1_*".match(pattern)); //=> _test1_*
alert("_test2_".match(pattern)); //=> _test2_
alert("*_test2_".match(pattern)); //=> *_test2_
alert("1_test1_".match(pattern)); //=> null
alert("a_test1_".match(pattern)); //=> null
alert("_test1_1".match(pattern)); //=> null
alert("_test1_a".match(pattern)); //=> null