如何测试字符串是否有数组条目
How to Test if a String Has an Array Entry
我正在用 Discord.js 创建一个 Discord 机器人,只要有人在消息中发誓,它就会捕捉到。我有一个数组,其中充满了我希望它能捕捉到的常见脏话、缩写、种族和性辱骂等。
const SwearWords = ["a##","ba##ard","bi###","c#ck","c#nt","d#ck","f#ck","gay","k#ke","n#gg","omfg","sh#t","wtf"];
(该数组没有所有主题标签,我只是为 post 添加了它们)
我最初尝试使用的是if (lcMsg.includes(SwearWords)) {return;}
,lcMsg
是message.content.toLowerCase();
,这样无论用户如何大写,它都能抓住用户的脏话。但这没有用,所以我尝试在 Google-ing 答案后使用 .entries()
和 .every()
(但我从未找到任何答案)。
我想 .map()
行得通吗?我不知道,因为我还没有学会如何使用它。如果有人能帮我解决这个问题那就太好了。
数组.some
方法在这里会有帮助。将它与您的 .includes
结合起来,看看邮件中是否出现了这些词:
const SwearWords = ["a##","ba##ard","bi###","c#ck","c#nt","d#ck","f#ck","gay","k#ke","n#gg","omfg","sh#t","wtf"];
const saltyMessage = "wtf, git gud scrub";
const niceMessage = "gg wp";
function hasBadWord(msg) {
return SwearWords.some(word => msg.includes(word));
}
console.log("Message and has swear word?:", saltyMessage, " -> ", hasBadWord(saltyMessage));
console.log("Message and has swear word?:", niceMessage, " -> ", hasBadWord(niceMessage));
此外,您可以使用 .find
而不是 .some
来查找消息包含的单词:
const SwearWords = ["a##","ba##ard","bi###","c#ck","c#nt","d#ck","f#ck","gay","k#ke","n#gg","omfg","sh#t","wtf"];
const saltyMessage = "wtf, git gud scrub";
const niceMessage = "gg wp";
function whichBadWord(msg) {
return SwearWords.find(word => msg.includes(word));
}
console.log("Message and has swear word?:", saltyMessage, " -> ", whichBadWord(saltyMessage));
console.log("Message and has swear word?:", niceMessage, " -> ", whichBadWord(niceMessage));
您需要使用函数some
和函数includes
lcMsg.replace(/\s+/g, ' ').split(' ').some((w) => SwearWords.includes(w));
看看变量 lcMsg
是如何准备循环它的词的。
我正在用 Discord.js 创建一个 Discord 机器人,只要有人在消息中发誓,它就会捕捉到。我有一个数组,其中充满了我希望它能捕捉到的常见脏话、缩写、种族和性辱骂等。
const SwearWords = ["a##","ba##ard","bi###","c#ck","c#nt","d#ck","f#ck","gay","k#ke","n#gg","omfg","sh#t","wtf"];
(该数组没有所有主题标签,我只是为 post 添加了它们)
我最初尝试使用的是if (lcMsg.includes(SwearWords)) {return;}
,lcMsg
是message.content.toLowerCase();
,这样无论用户如何大写,它都能抓住用户的脏话。但这没有用,所以我尝试在 Google-ing 答案后使用 .entries()
和 .every()
(但我从未找到任何答案)。
我想 .map()
行得通吗?我不知道,因为我还没有学会如何使用它。如果有人能帮我解决这个问题那就太好了。
数组.some
方法在这里会有帮助。将它与您的 .includes
结合起来,看看邮件中是否出现了这些词:
const SwearWords = ["a##","ba##ard","bi###","c#ck","c#nt","d#ck","f#ck","gay","k#ke","n#gg","omfg","sh#t","wtf"];
const saltyMessage = "wtf, git gud scrub";
const niceMessage = "gg wp";
function hasBadWord(msg) {
return SwearWords.some(word => msg.includes(word));
}
console.log("Message and has swear word?:", saltyMessage, " -> ", hasBadWord(saltyMessage));
console.log("Message and has swear word?:", niceMessage, " -> ", hasBadWord(niceMessage));
此外,您可以使用 .find
而不是 .some
来查找消息包含的单词:
const SwearWords = ["a##","ba##ard","bi###","c#ck","c#nt","d#ck","f#ck","gay","k#ke","n#gg","omfg","sh#t","wtf"];
const saltyMessage = "wtf, git gud scrub";
const niceMessage = "gg wp";
function whichBadWord(msg) {
return SwearWords.find(word => msg.includes(word));
}
console.log("Message and has swear word?:", saltyMessage, " -> ", whichBadWord(saltyMessage));
console.log("Message and has swear word?:", niceMessage, " -> ", whichBadWord(niceMessage));
您需要使用函数some
和函数includes
lcMsg.replace(/\s+/g, ' ').split(' ').some((w) => SwearWords.includes(w));
看看变量 lcMsg
是如何准备循环它的词的。