利用正则检测文中敏感词并标红

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title></title>

</head>

<body>

<div id="content">

老王张建东老刘账户老王你老张事属于朱新开赵德龙老王始看老王多好玩锁考试

</div>

<button id='btn'>检测</button>

<script>

var con=document.getElementById('content');

var val=con.innerHTML;

var btn=document.getElementById('btn');

var reg=['老王','老刘','老张'];

 

btn.onclick=filter;

 

function filter(){

for(var i=0;i<reg.length;i++){

val=val.replace(new RegExp(reg[i],'g'),function($0){return '<font color=red>' + $0 + '</font>'});

con.innerHTML=val;

};

};

</script>

</body>

</html>

 

posted @ 2017-03-17 16:39  千里独行0321  阅读(396)  评论(0编辑  收藏  举报