开发者

Regular expression for replacing profanity words in a string

I'm trying to replace a set of words in a text string. Now I have a loop, which does not perform well:

function clearProfanity(s) {
   var profanity = ['ass', 'bottom', 'damn', 'shit'];
   for (var i=0; i < profanity.length; i++) {
      s = s.replace(profanity[i], "###!");
   }
   return s;
}

I want something that works faster, and s开发者_高级运维omething that will replace the bad word with a ###! mark having the same length as the original word.


Here's one way to do it:

String.prototype.repeat = function(n){
    var str = '';
    while (n--){
        str+=this;
    }
    return str;
}

var re = /ass|bottom|damn|shit/gi
  , profane = 'my ass is @ the bottom of the sea, so shit \'nd damn';

alert(profane.replace(re,function(a) {return '#'.repeat(a.length)}));
//=>my ### is @ the ###### of the sea, so #### 'n ####

To be complete: here's a simpler way to do it, taking word boundaries into account:

var re = /\W+(ass|shit|bottom|damn)\W+/gi
      , profane = [ 'My cassette of forks is at the bottom'
                   ,'of the sea, so I will be eating my shitake'
                   ,'whith a knife, which can be quite damnable'
                   ,'ambassador. So please don\'t harrass me!'
                   ,'By the way, did you see the typo'
                   ,'in "we are sleepy [ass] bears"?']
                  .join(' ')
                  .replace( re, 
                              function(a){ 
                                return a.replace(/[a-z]/gi,'#'); 
                              } 
                   );
alert(profane);


See it working: http://jsfiddle.net/osher/ZnJ5S/3/

Which basically is:

var PROFANITY = ['ass','bottom','damn','shit']
  , CENZOR = ("#####################").split("").join("########")
  ;
PROFANITY  = new RegExp( "(\\W)(" + PROFANITY.join("|") + ")(\\W)","gi");

function clearProfanity(s){
    return s.replace( PROFANITY
                    , function(_,b,m,a) { 
                         return b + CENZOR.substr(0, m.length - 1) + "!" + a
                      } 
                    );
}


alert( clearProfanity("'ass','bottom','damn','shit'") );

It would be better if the PROFANITY array would be initiated as a string, or better - directly as a Regular Expression:

//as string
var PROFANITY = "(\\W)(ass|bottom|damn|shit)(\\W)";
PROFANITY = new RegExp(PROFANITY, "gi"); 

//as regexp
var PROFANITY = /(\W)(ass|bottom|damn|shit)(\W)/gi
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜