1.js 由于 敏感词多 倒是页面运行缓慢 所以最后还是放到c#后台实现过滤
var words=['敏感词1','敏感词2']; var p='我是敏感词1,我也是敏感词2,测试看看'; for(var i=0;i<words.length;i++){ p=p.replace(new RegExp(words[i],'ig'),new Array(words[i].length).join('*')); }; alert(p);
C#形式敏感词过滤
public bool Read(string txt)
{
string path = HttpContext.Current.Server.MapPath("~/Filter/Filter.txt");
Encoding code = Encoding.GetEncoding("gb2312");
StreamReader sr = new StreamReader(path, code);
string temp = Regex.Replace(txt, @"[^\u4E00-\u9FA5]", " ");
string tmepvalue = sr.ReadToEnd();
sr.Close();
bool change = false;
foreach (string s in tmepvalue.Split('|'))
{
string ci = s;
if (txt.Contains(ci))
{
temp = Regex.Replace(txt, ci, "**");
change = true;
}
}
if (change)
{
string errortxt = " <script> $('.success').hide(); $('.warning').show(); $('.intotit').html('警告'); " +
" $('.errorinfo').html('留言存在非法内容,请将**号位置重新修改。'); $('.showok').show();$('#messages').val('" + temp + "');</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(), "AA", errortxt);
return false;
}
return true;
}
//Page.ClientScript.RegisterStartupScript这种弹出 会让我的html输入框内容为空 只能 弹出后重新赋值一遍