導航:首頁 > 凈水問答 > 網站過濾代碼

網站過濾代碼

發布時間:2023-08-05 09:05:17

『壹』 asp.net如何過濾掉html代碼

Asp.net中如何過濾html,js,css代碼
以下為引用的內容:

#region/// 過濾html,js,css代碼
/// <summary>
/// 過濾html,js,css代碼
/// </summary>
/// <param name="html">參數傳入</param>
/// <returns></returns>
public static string CheckStr(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex1.Replace(html, ""); //過濾<script></script>標記
html = regex2.Replace(html, ""); //過濾href=javascript: (<A>) 屬性
html = regex3.Replace(html, " _disibledevent="); //過濾其它控制項的on...事件
html = regex4.Replace(html, ""); //過濾iframe
html = regex5.Replace(html, ""); //過濾frameset
html = regex6.Replace(html, ""); //過濾frameset
html = regex7.Replace(html, ""); //過濾frameset
html = regex8.Replace(html, ""); //過濾frameset
html = regex9.Replace(html, "");
html = html.Replace(" ", "");
html = html.Replace("</strong>", "");
html = html.Replace("<strong>", "");
return html;
}
#endregion
#region /// 過濾p /p代碼
/// <summary>
/// 過濾p /p代碼
/// </summary>
/// <param name="html">參數傳入</param>
/// <returns></returns>
public static string InputStr(string html)
{
html = html.Replace(@"\<img[^\>]+\>", "");
html = html.Replace(@"<p>", "");
html = html.Replace(@"</p>", "");
return html;
}
#endregion

『貳』 js過濾HTML標簽以及空格的思路及代碼

|

代碼如下:
function
setContent(str)
{
str
=
str.replace(/</?[^>]*>/g,'');
//去除HTML
tag
str.value
=
str.replace(/[
|
]*n/g,'n');
//去除行尾空白
//str
=
str.replace(/n[s|
|
]*r/g,'n');
//去除多餘空行
return
str;
}

測試的時候發現這段代碼不能過濾掉網頁中空格字元(即:
)。於是自己又改造了一下:
代碼如下:
function
removeHTMLTag(str)
{
str
=
str.replace(/</?[^>]*>/g,'');
//去除HTML
tag
str
=
str.replace(/[
|
]*n/g,'n');
//去除行尾空白
//str
=
str.replace(/n[s|
|
]*r/g,'n');
//去除多餘空行
str=str.replace(/
/ig,'');//去掉
return
str;
}

恩,我的要求達到了。
現在來稍稍解釋一下所用到的三個正則表達吧(需要說明的是,因為自己也是剛剛接觸,也許我的解釋並不是正確的,僅供參考):
第一個:/</?[^>]*>/g
在js中正則表達式是以“/”開頭的,後面的/g,含義是表示全局模式,意思是在將匹配的模式應用於整個字元串,而不是在第一次匹配上之後就停止匹配了。
</?[^>]*>
這個分開來解釋,其中第二個字元“”是一個轉移字元,用來轉移後面的”/”字元的。?匹配0或1個正好在它之前的那個字元。注意:這個元字元不是所有的軟體都支持的。所以</?就是匹配html標簽中的”</”格式或者“<”格式的。
再來說[^>]*>。[]是含義是:
^的含義是:匹配一行的開始。例如正則表達式^When
in能夠匹配字元串"When
in
the
course
of
human
events"的開始,但是不能匹配"What
and
When
in
the"。意思就是匹配以“When
in”開頭的文字。
*的含義是:匹配0或多個正好在它之前的那個字元。例如正則表達式。*意味著能夠匹配任意數量的任何字元
因此[^>]*意思是匹配>之外的字元。所以[^>]可以匹配出的模式可以像下面這樣的:
div
我需要的文字</div
我需要的文字</p
*和前面的[^>]結合在一起就可以匹配下面這些字元了:
div>我需要的文字</div
p>我需要的文字</p
br
/
再加上後面的>就可以匹配下面的字元了:
div>我需要的文字</div>
p>我需要的文字</p>
br
/>
這樣就完成了一對HTML標簽的匹配了。(多句話,總覺得這個匹配有點啰嗦,但是不知道到底在哪個地方啰嗦)
第二個:/[
|
]*n/g:我也沒有看懂
第三個:/
/ig:就是直接查找
字元,後面的/ig的含義是在全局模式下進行不區分大小寫的查找。g代表全局,i表示不區分大小寫。

『叄』 正則表達式 過濾網址

正則表達式,過濾出所有超鏈接除了一個url,例如:

<a href= 'http://www.abc.com/'> abc.com </a><br /><a href= 'http://www.edf.com/'> edf.com </a>

過濾:變為abc.com <br /><a href= 'http://www.edf.com/'> edf.com </a>沒人知道怎麼做么,要保留edf.com的超級鏈接,過濾掉其他的所有網址的超級鏈接。

FunctionautoLink(str)

Setra=NewRegExp

ra.IgnoreCase=True

ra.Global=True

ra.Pattern = "<a[^>]+>(.+?)</a>"

autoLink=ra.replace(str,"$1")

ENDFunction

(3)網站過濾代碼擴展閱讀:

注意事項:

正則表達式,也稱為正則表達式。這是計算機科學中的一個概念。

正則表達式通常用於檢索和替換符合模式(規則)的文本,許多編程語言都支持使用正則表達式進行字元串操作。

例如Perl中內置了一個強大的正則表達式引擎。正則表達式的概念最初是由諸如(sed和GREp)這樣的Unix工具推廣的。

正則表達式通常縮寫為「regex」。單數形式是regexp、regex,復數形式是regexps、regexes和regexen。

『肆』 怎樣用js方法過濾html等代碼

^<input type="text" id="theOne" value="">
<input type="button" onclick="NoHtml()" value="過濾html標簽">
<script>
function NoHtml(){
var t=document.getElementById("theOne").value;
t=t.replace(/({|})/g,''); //過濾{}
t=t.replace(/</g,'<'); //置換符號<
t=t.replace(/>/g,'>'); //置換符號>
// t=t.replace(/<\/?[^>]*>/g,''); //*<\/?[^>]*>可以匹配<script></style></body>等,並置空。而不是替內換容<和>兩個符號
document.getElementById("theOne").value=t;
}
</script>

閱讀全文

與網站過濾代碼相關的資料

熱點內容
污水泵有什麼品種 瀏覽:264
國產ro膜有哪些品牌 瀏覽:300
污水中phb是什麼 瀏覽:902
飲水機通電不工作是什麼原因 瀏覽:525
凈水器怎麼調水溫最合適 瀏覽:737
福特野馬用什麼空氣濾芯 瀏覽:616
小米攝像頭回看怎麼用 瀏覽:592
留置針回血過了夾子用生理鹽水回推 瀏覽:484
凈化器的cfm是什麼意思 瀏覽:407
凱美瑞汽油濾芯4s店多少錢 瀏覽:700
什麼污水不需要消毒處理 瀏覽:347
凈化器有氣泡怎麼回事 瀏覽:765
反滲透膜批發優選上海保茲 瀏覽:128
編制EDI應注意哪幾個方面的原則 瀏覽:772
陰樹脂一個立方有多重 瀏覽:349
純凈水口感不好是怎麼回事 瀏覽:379
從哈市回北安用隔離嗎 瀏覽:645
蘇州化工廢水會議 瀏覽:732
污水處理pac投加系統 瀏覽:563
西安第二污水處理廠形同虛設 瀏覽:529