如何测试字符串是否有数组条目

时间:2018-03-02 21:05:41

标签: javascript arrays node.js discord.js

我正在使用Discord.js创建一个Discord bot,每当有人发帖消息时都会捕获它。我有一个阵列,里面充满了常见的咒骂,缩写,种族和性辱骂等等,我希望它能够抓住它。

if (lcMsg.includes(SwearWords)) {return;}

(数组没有所有的标签,我只是将它们添加到帖子中)

我最初尝试使用的是lcMsg message.content.toLowerCase();.entries(),因此无论用户如何利用它们,都可以抓住用户的咒骂。但是这没有用,所以我在谷歌给出答案之后尝试使用.every().map()(我从未找到任何答案)。

我想import pandas as pd import numpy as np df = pd.DataFrame({ 'CITY' : np.random.choice(['PHOENIX','ATLANTA','CHICAGO', 'MIAMI', 'DENVER'], 1000), 'DAY': np.random.choice(['Monday','Tuesday','Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], 1000), 'LOCATION' : np.random.choice(['Location1','Location2','Location3', 'Location4', 'Location5'], 1000), 'COUNT': np.random.randint(1, 700, size=1000)}) print(df) CITY COUNT DAY LOCATION 0 DENVER 521 Wednesday Location4 1 MIAMI 123 Saturday Location3 2 ATLANTA 122 Wednesday Location3 3 DENVER 363 Saturday Location1 4 PHOENIX 656 Saturday Location4 5 PHOENIX 369 Saturday Location3 6 MIAMI 455 Tuesday Location5 会起作用吗?我不知道,因为我还没有学会如何使用它。如果有人能帮我解决这个问题,那就太好了。

2 个答案:

答案 0 :(得分:1)

数组.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));




答案 1 :(得分:0)

您需要使用函数some和函数includes

lcMsg.replace(/\s+/g, ' ').split(' ').some((w) => SwearWords.includes(w));

看看变量lcMsg是如何准备循环其单词的。