如何删除不包含字母数字字符的单词?

时间:2019-02-17 18:30:56

标签: javascript discord discord.js

如何删除不包含字母数字字符的单词?

但是用户可以使用bann.edWord1或类似版本来绕过此操作...

var badWords = [
  'bannedWord1',
  'bannedWord2',
  'bannedWord3',
  'bannedWord4'
];

bot.on('message', message => {
  var words = message.content.toLowerCase().trim().match(/\w+|\s+|[^\s\w]+/g);
  var containsBadWord = words.some(word => {
    return badWords.includes(word);
  });
  if (containsBadWord) {
    message.delete(1);
    message.channel.send("That word's not appropriate!");
  }
});

1 个答案:

答案 0 :(得分:0)

您可以使用建议的解决方案here从字符串中删除非字母数字字符,然后检查“坏词”。

示例:

var badWords = [
  'bannedWord1',
  'bannedWord2',
  'bannedWord3',
  'bannedWord4'
];

bot.on('message', message => {
  var textOnly = message.content.replace(/\W/g, '');

  for (i=0; i<badWords.length; i++) {
    if (textOnly.indexOf(badWords[i].replace(/\W/g, '')) !== -1) {
      message.delete(1);
      message.channel.send("That word's not appropriate!");
      break;
    }
  }
});

// bannedWord1 fill be filtered
// ban.ne.d Word 1 will be filtered, too