当前位置:Gxlcms > 正则表达式 > 用正则表达式过滤html代码

用正则表达式过滤html代码

时间:2021-07-01 10:21:17 帮助过:57人阅读

代码例子如下:
<%
Option Explicit

Function stripHTML(strHTML)
'Strips the HTML tags from strHTML

 Dim objRegExp, strOutput
 Set objRegExp = New Regexp

 objRegExp.IgnoreCase = True
 objRegExp.Global = True
 objRegExp.Pattern = "<.+?>"

 'Replace all HTML tag matches with the empty string
 strOutput = objRegExp.Replace(strHTML, "")

 'Replace all < and > with < and >
 strOutput = Replace(strOutput, "<", "<")
 strOutput = Replace(strOutput, ">", ">")

 stripHTML = strOutput 'Return the value of strOutput

 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>
 <%=StripHTML(Request("txtHTML"))%>
 </pre>
<% End If %>

人气教程排行