『壹』 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>