how can I do a jQuery swear word / bad word filter?

前端 未结 6 438
情话喂你
情话喂你 2020-12-05 05:53

I know there are many arguments as to why this is a bad idea, but in my implementation I\'m planning on enabling/disabling bad words in the account settings. In other words

6条回答
  •  感情败类
    2020-12-05 06:25

    Something like this might work:

    String.prototype.repeat = function(num){
      return new Array(num + 1).join(this);
    }
    
    var filter = ['ass', 'piss'];
    
    $('.post').text(function(i, txt){
    
      // iterate over all words
      for(var i=0; i

    Working example on jsFiddle

    Edit: Added boundaries so it won't replace words that contain the swear words. I've used double backslashes because backslashes should be escaped in a string, see this topic.

提交回复
热议问题