Simulate a simple sensitive word filter with js

1, with a simple analog js sensitive word filter
(word sensitive user input is replaced with *)
Example: . "Today, there is a fool next to yelling affected my operation" ,
after the filter: " Today there was someone yelling next to me, which affected my work."

Effect picture
html code:

<input type="text" placeholder="请输入一句话" id="SRtwo">
<input type="button" onclick="two()" value="过滤">
<input type="text" readonly="readonly" id="SCtwo">

js code:

 function two(){
        var SRtwo = document.getElementById("SRtwo").value;   //获取输入文本框
        var SCtwo = document.getElementById("SCtwo");  //获取输出文本框
        var arr = ["傻子","傻叉","操","他妈的","tmd","TMD","nnd"]; //定义一个敏感词汇数组
        var showtwo = SRtwo;   //将获得到的文本框的值用一个变量接收
        for(var i=0;i<arr.length;i++){        //将数组arr里面的内容遍历一遍
            var r = new RegExp(arr[i],"ig")        //新建一个全局正则表达式
            showtwo = showtwo.replace(r,"*");   //替换文本框输入的敏感词
        }
        SCtwo.value = showtwo;        //进行文本框输出
    }

Guess you like

Origin blog.csdn.net/qq_43923146/article/details/107342285