我想从推文列表中过滤掉单词,如果数组中出现两个以上的单词,则退出循环。字符串数组匹配字符串
说我有一个字符串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