在html中为我的输入实现错误的单词过滤器?

时间:2016-01-30 17:54:04

标签: javascript string

我打算将这个坏词列表(https://github.com/shutterstock/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/blob/master/en)放到我的Web服务器上的.txt文件中。我怎么能这个javascript检查变量" userNickName"针对名为" blacklist.txt"的.txt文件在我的网络服务器上。

(这是我想要实现坏词检查的代码,我该怎么做?)

if (wsIsOpen() && null != userNickName) {
  var msg = prepareData(1 + 2 * userNickName.length);
  msg.setUint8(0, 0);
  for (var i = 0; i < userNickName.length; ++i) msg.setUint16(1 + 2 * i, userNickName.charCodeAt(i), true);
  wsSend(msg)

2 个答案:

答案 0 :(得分:1)

创建一个像badWords = ["boob","boobs","..."];

这样的JS数组

可以使用Get file contents in java scriptHow do I split a string, breaking at a particular character?

完成此操作

或者直接在服务器端使用PHP。

$badWords = file_get_contents('https://raw.githubusercontent.com/shutterstock/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words/master/en');

$badWords = explode("\n", $badWords);
echo 'badWords = ["'.implode('","', $badWords).'"];';

之后,How to find if an array contains a specific string in JavaScript/jQuery?(非JQuery的第二个答案)

答案 1 :(得分:0)

message.save((err) => {
    if (err)
        sendStatus(500)

    Messages.findOne({ message: 'badword' }, (err, censored) => {
        if (censored) {
            console.log('cendsored words found', censored)

            message.remove({ _id: censored.id }, (err) => {
                console.log('removed censored message')
            })
        }
    })

    io.emit('message', req.body)
    res.sendStatus(200);
})## Heading ##