用于替换字符串中的脏话的正则表达式

发布于 2024-10-21 21:21:23 字数 330 浏览 8 评论 0原文

我正在尝试替换文本字符串中的一组单词。现在我有一个循环,它的性能不佳:

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'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 something that will replace the bad word with a ###! mark having the same length as the original word.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2024-10-28 21:21:23

这是一种方法:

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 ####

为了完整起见:这是一种更简单的方法,考虑到单词边界:

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);

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);
望笑 2024-10-28 21:21:23

看看它的工作原理:
http://jsfiddle.net/osher/ZnJ5S/3/

基本上是

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'") );

:如果 PROFANITY 数组作为字符串启动会更好,或者直接作为正则表达式启动更好:

//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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文