我想從推文列表中過濾掉單詞,如果數組中出現兩個以上的單詞,則退出循環。字符串數組匹配字符串
說我有一個字符串I was following someone the other day that looked a bit grumpy
我有一個字符串數組:
[
"followback",
"followers",
"grumpy cat",
"gamergate",
"quotes",
"facts",
"harry potter"
]
有沒有一種方法,我可以匹配它不會通過.indexOf
這將只匹配拾起短語grumpy cat
grumpy
?
const yourstring = 'I was following someone the other day that looked a bit grumpy'
const substrings = [
"followback",
"followers",
"grumpy cat",
"gamergate",
"quotes",
"facts",
"harry potter"
]
let len = substrings.length;
while(len--) {
if (yourstring.indexOf(substrings[len])!==-1) {
console.log('matches: ', substrings[len])
}
}
你想馬赫'grumpy'或'cat'? – baao
那麼,在一個字符串中使用「脾氣暴躁的貓」是什麼呢?爲什麼不把它作爲兩個單獨的單詞存儲在你的數組中? – trincot
你有什麼要告訴我們_兩個以上的word_先決條件? – baao