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

相关文章:

  • 2021-03-31
  • 2022-12-23
  • 2021-09-26
  • 2021-10-22
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-01
  • 2022-02-04
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2021-05-09
相关资源
相似解决方案