我发现jquery代码(我忘了原来的网站)正在努力将html页面中的单词替换为星号(*),并且代码运行良好,但代码只能用于替换每个单个单词,不能更改单词的部分并且还区分大小写。用jquery替换html页面中的单词或一些字符
jQuery代码:
String.prototype.repeat = function(num){
return new Array(num + 1).join(this);
}
/* Word or Character to be replace */
var filter = ['itch','asshole', 'uck', 'sex'];
$('body').text(function(i, txt){
// iterate over all words
for(var i=0; i<filter.length; i++){
// Create a regular expression and make it global
var pattern = new RegExp('\\b' + filter[i] + '\\b', 'g');
// Create a new string filled with '*'
var replacement = '*'.repeat(filter[i].length);
txt = txt.replace(pattern, replacement);
}
// returning txt will set the new text value for the current element
return txt;
});
词过滤:
['itch','asshole', 'uck', 'sex'];
和结果:
sex -> *** // successfully replacing
SEX -> SEX // not replaced, i want this word also replaced to ***
bitch -> bitch // not replaced, i want this word replaced to b****
如何修改这个jQuery代码,以便可以用来改变一些字中的字符而不区分大小写?
小提琴:http://jsfiddle.net/bGhq8/
谢谢。
“本周早些时候,我在一次关于我的新项目的会议上发了言。” “我的皮肤是\ *** \ * Y”。 “通过我* \ * *,我设法通过了考试”。 “他去k \ *** \ * en拿到一些食物” – nhahtdh 2013-04-10 01:59:18
@nhahtdh:是这样的。喜欢:“什么F ***,这****如此闻”。 – Fredy 2013-04-10 02:04:00
我的意思是说明你正在审查那些不应该被审查的事情。 – nhahtdh 2013-04-10 02:07:02