C# 过滤敏感字符

今天在做的过滤特殊字符中,有多个单词组成的(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

原文地址:https://www.cnblogs.com/booth/p/2269552.html