您的位置:首页 > 编程语言 > C#

C# 过滤敏感字符

2011-11-30 19:55 387 查看
今天在做的过滤特殊字符中,有多个单词组成的(butt plug)、中间有*号(f**k)的和短单词的(hell),比如“butt plug”等,用下面的正则就搞定了

public JsonResult BadWords(string content)
{
var badWords = new[] { "java", "oracle", "webforms" };
if (CheckText(content, badWords))
{
return Json("Sorry, you can't use java, oracle or webforms!", JsonRequestBehavior.AllowGet);
}
return Json(true, JsonRequestBehavior.AllowGet);
}

private bool CheckText(string content, string[] badWords)
{
foreach (var badWord in badWords)
{
var regex = new Regex("(^|[\\?\\.,\\s])" + badWord + "([\\?\\.,\\s]|$)"); //注意 badWord里面有正则特殊字符需要用@"\$"代替
if (regex.IsMatch(content)) return true;
}
return false;
}


代码原文在
http://stackoverflow.com/questions/7266354/how-to-filter-bad-words-of-textbox-in-asp-net-mvc
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: