我的模式在第一次匹配後停止,並且在那裏有全局匹配。Javascript正則表達式數組不匹配
//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_」 ,我想在下面(第二碼塊)保持在相同(如commentend部分所示)。
我不知道爲什麼_test2_不匹配,因爲它與下面顯示的測試完全匹配。
問題FINISH
以下是測試和工作,他們應該。
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
你的問題到底是什麼?你可以編輯你的問題,讓實際問題更清楚嗎? – Ivan
最上面的代碼給出了一個包含\ _test1_ *的數組,但我希望它具有[0] = \ _test1_ *和[1] = \ _test2_ – Reuben1st
這只是令人困惑。請不要發佈,除非你有一個很好的說明你想要什麼。然後繼續嘗試構建你的問題,這是可以理解的。 (只是稍微瞭解你的問題的線索,我必須在你的問題中瘋狂地跳起來) –