溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#過濾關(guān)鍵字,js過濾關(guān)鍵字

發(fā)布時間:2020-08-11 16:53:12 來源:網(wǎng)絡(luò) 閱讀:579 作者:365850153 欄目:開發(fā)技術(shù)

C#:后臺過濾關(guān)鍵字    

    //過濾關(guān)鍵字【區(qū)分大小寫】

        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;

        }


    //過濾關(guān)鍵字【不區(qū)分大小寫】

        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:前端過濾關(guān)鍵字【不區(qū)分大小寫過濾的把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++) {

                    //替換為空【重復(fù)多次出現(xiàn)也會過濾掉】

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

                }

                //替換為空的【重復(fù)多次出現(xiàn)也會過濾掉】

                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 不區(qū)分大小寫的匹配
/regexp/s 使句點(.)匹配任何字符,包括換行符(\n)
/regexp/x 從模式中刪除空白符和注釋
/regexp/m 使^匹配換行符 (\n)之后的內(nèi)容,美元符號($)匹配換行符 (\n)之前的內(nèi)容
/regexp/e 如果替換字符串是
PHP代碼,使用eval()執(zhí)行該代碼來得到實際的替換字符串。
 
PHP的Perl兼容正則表達式函數(shù)也支持在Perl中不支持的其他修飾符,如表4-13所示:
表4-13:其他的PHP標(biāo)志
修飾符 意 義
/regexp/U 顛倒子模式的貪婪性;*和+盡可能少地匹配而不是盡可能多。
/regexp/u 把模式字符串當(dāng)作UTF-8編碼對待
/regexp/X 如果一個反斜杠之后跟著沒有特殊意義的字符,將產(chǎn)生一個錯誤
/regexp/A 把錨定位在字符串的開頭就像模式中有^一樣
/regexp/D 使$字符僅匹配一行的末尾
/regexp/S 使表達式解析器更加小心地檢查模式的結(jié)構(gòu),使得第二次運行時(如在一個循環(huán)中)加快速度


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI