我發現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