C# 正则过滤html,js,css代码 - TOMMYHU - 专注互联网开发及运营技术,提供相关资料及软件下载,奇趣网络时事评论!
Aug 6

C# 正则过滤html,js,css代码 不指定

tommyhu , 11:53 , 大前端 » CSS3 , Comments(1) , Trackbacks(0) , Reads(15459) , Via Original Large | Medium | Small
C# 正则过滤html,js,css代码

public static string InputText(string text)//过滤html,js,css代码
    {
        text = text.Trim();
        if (string.IsNullOrEmpty(text))
            return string.Empty;
        text = Regex.Replace(text, "[\\s]{2,}", " "); //两个或多个空格替换为一个
        text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
        text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //&nbsp;
        text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //其它任何标记
        text = text.Replace("'", "''");
        return text;
    }


C# 正则过滤html,js,css代码


另外大家也可以看看
asp正则过滤html,js,css代码

<%
Option Explicit

Function RemoveHTML(strHTML) '过滤HTML代码的函数包括过滤CSS和JS

StrHtml = Replace(StrHtml,vbCrLf,"")
StrHtml = Replace(StrHtml,Chr(13)&Chr(10),"")
StrHtml = Replace(StrHtml,Chr(13),"")
StrHtml = Replace(StrHtml,Chr(10),"")
StrHtml = Replace(StrHtml," ","")
StrHtml = Replace(StrHtml," ","")

Dim objRegExp, Match, Matches
Set objRegExp = New Regexp

objRegExp.IgnoreCase = True
objRegExp.Global = True

'取闭合的<>
objRegExp.Pattern = "<style(.+?)/style>"
'进行匹配
Set Matches = objRegExp.Execute(strHTML)

' 遍历匹配集合,并替换掉匹配的项目
For Each Match in Matches
strHtml=Replace(strHTML,Match.Value,"")
Next

'取闭合的<>
objRegExp.Pattern = "<script(.+?)/script>"
'进行匹配
Set Matches = objRegExp.Execute(strHTML)

' 遍历匹配集合,并替换掉匹配的项目
For Each Match in Matches
strHtml=Replace(strHTML,Match.Value,"")
Next

'取闭合的<>
objRegExp.Pattern = "javascript:(.+?)>"
'进行匹配
Set Matches = objRegExp.Execute(strHTML)

' 遍历匹配集合,并替换掉匹配的项目
For Each Match in Matches
strHtml=Replace(strHTML,Match.Value,"")
Next

'取闭合的<>
objRegExp.Pattern = "<.+?>"
'进行匹配
Set Matches = objRegExp.Execute(strHTML)

' 遍历匹配集合,并替换掉匹配的项目
For Each Match in Matches
strHtml=Replace(strHTML,Match.Value,"")
Next

RemoveHTML=strHTML
Set objRegExp = Nothing
End Function

%>

<form method="post" id=form1 name=form1>
<b>Enter an HTML String:</b><br>
<textarea name="txtHTML" cols="50" rows="8" wrap="virtual"><%=Request("txtHTML")%></textarea>
<p>
<input type="submit" value="Strip HTML Tags!" id=submit1 name=submit1>
</form>

<% if Len(Request("txtHTML")) > 0 then %>
<p><hr><p>
<b><u>View of string <i>with no</i> HTML stripping:</u></b><br>
<xmp>
<%=Request("txtHTML")%>
</xmp><p>
<b><u>View of string <i>with</i> HTML stripping:</u></b><br>
<pre>
<%=RemoveHTML(Request("txtHTML"))%>
</pre>
<% End If %>

另外值得注意的是,如果 发布帖子的人 发布的本就含有代码,将会是过滤更加苦难,可以做的是,例如
xx程序员在论坛中可视化编辑器的 视图状态 发帖

</xmp><p>
<b><u>View of string <i>with</i> HTML stripping:</u></b><br>
<pre>
<%=RemoveHTML(Request("txtHTML"))%>
是什么意思!
这样也会被过滤,具体解决方法有待商榷!

▲返回顶部
Last modified by tommyhu on2010/08/06 23:03

互联网开发网友
2012/05/31 15:44
来看看好朋友了,还记得我吗,要回访哦 http://www.weiqc.com/?10009
微启创-微博投放|微博广告-效果最好的微博精准投放平台
Pages: 1/1 First page 1 Final page
Add a comment

Nickname

emotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemot