html:
<textarea rows="10" cols="100" id="myDiv"></textarea>
<button id="getFile" οnclick="yz()">校验文字</button>
js:
//禁止多次请求
let stRs=sessionStorage.getItem('stRs')?sessionStorage.getItem('stRs'):loadXMLDoc();
//从菜鸟教程摘的简易ajax-get读文件
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) {
// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
xmlhttp = new XMLHttpRequest();
}
else {
// IE6, IE5 浏览器执行代码
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
stRs=(xmlhttp.responseText).replace(/\s+/g,'|');
sessionStorage.setItem("stRs",stRs);
}
}
xmlhttp.open("GET", "./CensorWords.txt", true);
xmlhttp.send();
}
//校验方法
function yz () {
var s=(document.getElementById("myDiv").value).trim();
if (s!==''){
//console.log(stRs);
var re = new RegExp(stRs, "g");
if(s.match(re)){
console.log("%c敏感词汇将被替换为:","color:red",s.replace(re,'?'));
}else {
console.log(s);
}
}else {
alert('内容为空怎校验?')
}
}
敏感词汇文件下载地址:https://pan.baidu.com/s/16Mt0hcanDkeZ_ya2FhZT6Q 密码:h788