C#:后台过滤关键字

//过滤关键字【区分大小写】

public string HtmlEscapeCode(string html)

{

var strhtml = html.Replace("javascript", "")

.Replace("vbscript", "")

.Replace("jscript", "")

.Replace("script", "")

.Replace("eval", "")

.Replace("<", "<")

.Replace(">", ">")

.Replace("\'", "'")

.Replace("\"", """)

.Replace("&", "&")

.Replace("#", "#");

return strhtml;

}


//过滤关键字【不区分大小写】

public string HtmlEscapeCode(string html)

{


var newstrhtml = Regex.Replace(html, "javascript", "*", RegexOptions.IgnoreCase);

newstrhtml = Regex.Replace(newstrhtml, "vbscript", "*", RegexOptions.IgnoreCase);

newstrhtml = Regex.Replace(newstrhtml, "jscript", "*", RegexOptions.IgnoreCase);

newstrhtml = Regex.Replace(newstrhtml, "script", "*", RegexOptions.IgnoreCase);

newstrhtml = Regex.Replace(newstrhtml, "eval", "*", RegexOptions.IgnoreCase);

newstrhtml = Regex.Replace(newstrhtml, "alert", "*", RegexOptions.IgnoreCase);

newstrhtml = Regex.Replace(newstrhtml, "<", "<", RegexOptions.IgnoreCase);

newstrhtml = Regex.Replace(newstrhtml, ">", ">", RegexOptions.IgnoreCase);

return newstrhtml;

}



js:前端过滤关键字【不区分大小写过滤的把gm替换成gi即可】

var keyWordsList = ["javascript", "vbscript", "jscript", "script", "eval"];

var keyWordsList1 = ["<", ">", "\'", "\"", "&", "#"];

var strRemark = $.trim($("textarea[remark=" + id + "]").val());//字符串值

var filtRemark = strRemark.replace(/[\r\n]/g, "");


for (var j = 0; j < keyWordsList.length; j++) {

//替换为空【重复多次出现也会过滤掉】

filtRemark = filtRemark.replace(new RegExp(keyWordsList[j], 'gm'), '');

}

//替换为空的【重复多次出现也会过滤掉】

filtRemark = filtRemark.replace(new RegExp(keyWordsList1[0], 'gm'), '<');

filtRemark = filtRemark.replace(new RegExp(keyWordsList1[1], 'gm'), '>');

filtRemark = filtRemark.replace(new RegExp(keyWordsList1[2], 'gm'), ''');

filtRemark = filtRemark.replace(new RegExp(keyWordsList1[3], 'gm'), '"');

filtRemark = filtRemark.replace(new RegExp(keyWordsList1[4], 'gm'), '&');

filtRemark = filtRemark.replace(new RegExp(keyWordsList1[5], 'gm'), '#');



/regexp/i不区分大小写的匹配
/regexp/s使句点(.)匹配任何字符,包括换行符(\n)
/regexp/x从模式中删除空白符和注释
/regexp/m使^匹配换行符 (\n)之后的内容,美元符号($)匹配换行符 (\n)之前的内容
/regexp/e如果替换字符串是PHP代码,使用eval()执行该代码来得到实际的替换字符串。

PHP的Perl兼容正则表达式函数也支持在Perl中不支持的其他修饰符,如表4-13所示:
表4-13:其他的PHP标志
修饰符意 义
/regexp/U颠倒子模式的贪婪性;*和+尽可能少地匹配而不是尽可能多。
/regexp/u把模式字符串当作UTF-8编码对待
/regexp/X如果一个反斜杠之后跟着没有特殊意义的字符,将产生一个错误
/regexp/A把锚定位在字符串的开头就像模式中有^一样
/regexp/D使$字符仅匹配一行的末尾
/regexp/S使表达式解析器更加小心地检查模式的结构,使得第二次运行时(如在一个循环中)加快速度