我創造了我尋找的字符串正則表達式匹配:使用正則表達式來一個字符串數組
var re = new RegExp(searchTerm, "ig");
,我有,我想通過搜索數組有以下方面:
var websiteName = [
"google", "youtube", "twitter", "medium", "amazon", "airbnb", "campaiyn", "uber", "dropbox", "asana",
"slack", "soundcloud", "reddit", "uscitp", "facebook"
];
如果我的搜索詞是reddit testtest test
,當我打電話的匹配功能我不會匹配:
for(var i = 0; i < websiteName.length; i = i + 1) {
if(websiteName[i].match(re) != null) {
possibleNameSearchResults[i] = i;
}
}
我如何構造我的正則表達式,以便當我搜索數組時,如果只有一個單詞匹配,它仍然會返回true?
爲什麼不遍歷websiteName數組並使用每個元素作爲字符串的正則表達式搜索項。 – nick