且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#过滤关键字,js过滤关键字

更新时间:2022-09-27 15:06:48

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 使表达式解析器更加小心地检查模式的结构,使得第二次运行时(如在一个循环中)加快速度

本文转自程序猿博客51CTO不可靠,原文链接http://blog.51cto.com/haihuiwei/1956315如需转载请自行联系原作者

365850153