gpt4 book ai didi

jquery - 我怎样才能做一个 jQuery 脏话/坏话过滤器?

转载 作者:行者123 更新时间:2023-12-03 22:30:05 29 4
gpt4 key购买 nike

我知道对于为什么这是一个坏主意有很多争论,但在我的实现中,我计划在帐户设置中启用/禁用坏词。换句话说,坏词默认是可见的,但如果询问则关闭/隐藏。

计划是向客户端发送一个 JSON 字符串,让客户端过滤掉坏词。

json 字符串

['swear1', 'swear2']

原词

this phrase includes swear1

最终输出

this phrase includes ****

这是我迄今为止尝试过的

    $(document).ready (function () {
$('body').html().replace('asdf', 'ffff');
});

现在顺便说一下,我正在使用 asp.net mvc,我“可以”在服务器端执行此操作,但我认为如果卸载到客户端,这会更好......我愿意对此提出建议。

最佳答案

像这样的东西可能会起作用:

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

工作中example关于 jsFiddle

编辑:添加了边界,因此不会替换包含脏话的单词。我使用了双反斜杠,因为反斜杠应该在字符串中转义, see this topic .

关于jquery - 我怎样才能做一个 jQuery 脏话/坏话过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4541070/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com