0

我正在制作一个简单的 socket.io 应用程序,您可以在https://backchannel.glitch.me看到它。我正在尝试使用 RegExp 为该站点添加一个相对简单的亵渎过滤器,但我一直遇到问题 - 当检测到坏词时,没有显示我想要的消息,没有任何反应。我对 JS 和正则表达式非常缺乏经验,所以我可能忽略了一些东西,我将不胜感激。

我的代码:

<!doctype html>
<html>
  <head>
  <script>
  if (location.protocol != 'https:') {
   location.href = 'https://backchannel.glitch.me'
  }
</script>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" type="image/x-icon" href="https://cdn.glitch.com/a359ff0b-1273-40ab-a005-10968e106f1d%2Fimg_54456.png?1544043684904"/>
    <title>Backchannel</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; text-align: left; overflow: auto; color: White;}
      form { background: #666666; padding: 3px; position: static; bottom: 0; width: 100%; }
      form input { padding:  8px; width: 90%; margin-right: .5%;}
      form button { width: 9%; background: White; border: none; padding: 9px 8px; }
      #messages { list-style-type: none; margin: 0; padding: 0; max-height: 95.7vh; overflow:}
      #messages li { padding: 5px 10px; background: none;}
      #messages li:nth-child(odd) { background: none; }
      main {text-align: center; margin: 0; } 
      .messageContainer {overflow: auto;}
    </style>
<head>
</head>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var audio = new Audio('https://cdn.glitch.com/a359ff0b-1273-40ab-a005-10968e106f1d%2FError-sound.mp3?1544050723783');
$(function () {
    var socket = io();
    $('form').submit(function(){
      socket.emit('chat message', $('#m').val());
      $('#m').val('');
      return false;
    });
    socket.on('chat message', function(msg){
      //if a user types a message containing a "bad word", do not print it and instead print a system message saying it was blocked.
      if (msg == new RegExp('*bad words*')) {
      $('#messages').append($('<li>').text('[SYSMOD]: Message blocked due to explicit content'));
      }
      else
      $('#messages').append($('<li>').text(' [USER]: ' + msg));
      audio.play();
    });
//When the server emits " new user connected", add a message which says so.
  socket.on('new user', function(msg){
      $('#messages').append($('<li>').text('[SYSTEM]: New User Connected'));
      audio.play();
    });
//When the server emits "user disconnected", add a message which says so.
  socket.on('user disconnected', function(msg){
      $('#messages').append($('<li>').text('[SYSTEM]: User Disconnected'));
      audio.play();
    });
  });
  </script> 
  <form action="">
    <input id="m" autocomplete="off" /><button>Send Message</button>
    </form>
  </head>
  <body background="https://cdn.glitch.com/a359ff0b-1273-40ab-a005-10968e106f1d%2Fspacewinter1.jpg?1545146217248">
  <div id="main">
<div class="messageContainer">
      <ul id="messages"></ul>
    </div>
  </div>
  </body>
</html>

4

1 回答 1

1

将字符串与带有布尔运算符的 RegExp 对象进行比较将始终返回 false。您想为此使用testRegExp 的方法。例如:

new RegExp("*bad words*").test(msg);

此外,还存在用于构造正则表达式的简写语法。

/myregex/.test(msg);
于 2018-12-23T23:15:13.793 回答