asp去掉html,保留img br p div的正则实现代码

ASP 过滤HTML标签只剩<p> <br> <img>

<%
Function ReplaceContent(ContentStr)
Dim ClsTempLoseStr,regEx
If Isnull(ContentStr) Or Trim(ContentStr)="" Then
ReplaceContent=""
Exit Function
End If
ContentStr = Replace(ContentStr, "<Br />", "[Br]")
ContentStr = Replace(ContentStr, "<Br>", "[Br]")
ContentStr = Replace(ContentStr, "<P>", "[P]")
ContentStr = Replace(ContentStr, "</P>", "[/P]")
ClsTempLoseStr = Cstr(ContentStr)

Set regEx = New RegExp
regEx.Pattern = "<[^>]+>"
regEx.IgnoreCase = True
regEx.Global = True
ClsTempLoseStr = regEx.Replace(ClsTempLoseStr,"")
ClsTempLoseStr = Replace(ClsTempLoseStr, "[Br]", "<br />")
ClsTempLoseStr = Replace(ClsTempLoseStr, "[P]", "<P>")
ClsTempLoseStr = Replace(ClsTempLoseStr, "[/P]", "</P>")
ReplaceContent = ClsTempLoseStr
Set regEx = Nothing
End Function

Function RemoveP(strText)
Dim RegEx
Set RegEx=New RegExp
RegEx.IgnoreCase=True
RegEx.Global=True
RegEx.Pattern="<p [^<]*(.*)[^>]*>"
RemoveP=RegEx.Replace(strText,"[P]")
End Function

Function RemoveImg(strText)
Dim RegEx
Set RegEx=New RegExp
RegEx.IgnoreCase=True
RegEx.Global=True
RegEx.Pattern="<img [^<]*src=\'#\'" /> RemoveImg=RegEx.Replace(strText,"{img src=\'#\'" border=0}")
End Function

Function ImgRemove(strText)
Dim RegEx
Set RegEx=New RegExp
RegEx.IgnoreCase=True
RegEx.Global=True
RegEx.Pattern="{img [^{]*src=\'#\'" /> ImgRemove=RegEx.Replace(strText,"<img src='$1' border='0'>")
End Function

Response.Write(ImgRemove(ReplaceContent(RemoveImg(RemoveP(html)))))
%>

你可能感兴趣的:(img,div,正则,BR,p)