我知道有很多争论为什么这是一个坏主意,但在我的实现中,我计划在帐户设置中启用/禁用坏词.换句话说,默认情况下会显示坏词,但如果询问则会关闭/隐藏.
计划是将JSON字符串发送到客户端,让客户端过滤掉坏字.
json字符串
['swear1', 'swear2']
Run Code Online (Sandbox Code Playgroud)
原来的短语
this phrase includes swear1
Run Code Online (Sandbox Code Playgroud)
最终输出
this phrase includes ****
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所尝试的
$(document).ready (function () {
$('body').html().replace('asdf', 'ffff');
});
Run Code Online (Sandbox Code Playgroud)
现在请注意,我正在使用asp.net mvc而我"可以"在服务器端执行此操作,但我认为如果卸载到客户端会更好...我对这方面的建议持开放态度.
Har*_*men 12
像这样的东西可能会起作用:
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<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;
});
Run Code Online (Sandbox Code Playgroud)
关于jsFiddle的工作示例
编辑:添加边界,以便它不会替换包含咒骂词的单词.我使用了双反斜杠,因为反斜杠应该在字符串中转义,请参阅此主题.
| 归档时间: |
|
| 查看次数: |
11032 次 |
| 最近记录: |