开发程序,经常要用到正则表达式进行过滤一些不需要的东西,比如html js style div font,有时候需要过滤极个别的,有时候需要过滤好几种,不管怎么过滤,万变不离其宗。
这是我收藏的一些过滤函数,可以用来过滤您不需要的代码。如果需要过滤多种,可以嵌套使用,也可以自己整合代码。不过不建议嵌套使用,因为那样效率太低。
Asp 正则表达式 过滤 所有 html 标记 :
Function LoseHtml(ContentStr)
Dim ClsTempLoseStr,regEx
ClsTempLoseStr = Cstr(ContentStr)
Set regEx = New RegExp
regEx.Pattern = "<\/*[^<>]*>"
regEx.IgnoreCase = True
regEx.Global = True
ClsTempLoseStr = regEx.Replace(ClsTempLoseStr,"")
LoseHtml = ClsTempLoseStr
End function
Asp 正则表达式 过滤 style 标记 :
regEx.Pattern = "(<style)+[^<>]*>[^\0]*(<\/style>)+"
Asp 正则表达式 过滤 层 div 标记 :
regEx.Pattern = "<(\/){0,1}div[^<>]*>"
Asp 正则表达式 过滤 链接 a 标记 :
regEx.Pattern = "<(\/){0,1}a[^<>]*>"
Asp 正则表达式 过滤 字体 font 标记 :
regEx.Pattern = "<(\/){0,1}font[^<>]*>"
Asp 正则表达式 过滤 span 标记 :
regEx.Pattern = "<(\/){0,1}span[^<>]*>"
Asp 正则表达式 过滤 object 标记 :
regEx.Pattern = "<object.*?/object>"
Asp 正则表达式 过滤 iframe 标记:
regEx.Pattern = "(<iframe){1,}[^<>]*>[^\0]*(<\/iframe>){1,}"
Asp 正则表达式 过滤 script :
regEx.Pattern = "(<script){1,}[^<>]*>[^\0]*(<\/script>){1,}"
Asp 正则表达式 过滤 Class 标记 :
regEx.Pattern = "(class=){1,}(""|\'){0,1}\S+(""|\'|>|\s){0,1}"
字符串替换 Replace 的正则表达式 :
<%
Function ReplaceReg(str,patrn,replStr,Ignor)
'=========================================
'参数解释:
'str 原来的字符串
'patrn 要替换的字符串(正则表达式)
'replStr 要替换成的字符串
'Ignor 是否区分大小写(1不区分,0区分)
'=========================================
Dim regEx ' 建立变量。
If Ingor=1 Then Ingor=true else Ingor=false
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = Ignor ' 设置是否区分大小写。
regEx.Global=True
ReplaceReg = regEx.Replace(str,replStr) ' 作替换。
End Function
'例如 将 www.gdage.com 替换成 <a href="http://www.gdage.com ">www.gdage.com </a> _fcksavedurl=""http://www.gdage.com ">www.gdage.com </a>"
Response.Write(ReplaceReg("www.gdage.com ","www\.gdage\.com ","<a href=""http://www.gdage.com "">www.gdage.com </a>",1))
%>