过滤超级链接 复制代码 代码如下: Function RegRemoveHref(HTMLstr) Set ra = New RegExp ra.IgnoreCase = True ra.Global = True ra.Pattern = "<A[^>]+>(.+?)</A>" RegRemoveHref = ra.replace(HTMLstr,"$1") END Function
过滤所有HTML代码 复制代码 代码如下: Function RemoveHTML(strHTML) Dim objRegExp, Match, Matches Set objRegExp = New Regexp objRegExp.IgnoreCase = True objRegExp.Global = True "取闭合的<> 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
过滤所有HTML代码 和空格换行 复制代码 代码如下: Function RemoveHTML(strHTML) Dim objRegExp, Match, Matches Set objRegExp = New Regexp objRegExp.IgnoreCase = True objRegExp.Global = True objRegExp.Pattern = "<.+?>" "objRegExp.Pattern = "(
|
|
| | | )" 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
asp使用正则表达式去除script代码和HTML代码 一、清楚内容中的Javsscript 代码 这个代码的作用是去掉用<script </script>标记包含的所有部分。 根据实际需要,它也许不能满足要求。如果用在屏蔽客户提交代码的地方,应保证这一步在最后执行。 很多人还会拼凑这样的标记,应小心。 复制代码 代码如下: Function ClearJSCode(originCode) Dim reg set reg = New RegExp reg.Pattern = "<SCRIPT[^<]*</SCRIPT>" reg.IgnoreCase = True reg.Global = True clearJSCode = reg.Replace(originCode, "") End Function
二、清除内容中的HTML代码 复制代码 代码如下: Function ClearHTMLCode(originCode) Dim reg set reg = new RegExp reg.Pattern = "<[^>]*>" reg.IgnoreCase = True reg.Global = True ClearHTMLCode = reg.Replace(originCode, "") End Function
开发程序,经常要用到正则表达式进行过滤一些不需要的东西,比如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)+[^<>]*>[^ ]*(</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,}[^<>]*>[^ ]*(</iframe>){1,}" Asp 正则表达式 过滤 script : regEx.Pattern = "(<script){1,}[^<>]*>[^ ]*(</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.xxx.com 替换成 <a href="http://www.jb51.net">www.jb51.net</a> Response.Write(ReplaceReg("脚本之家www.xxx.com","www.xxx.com","<a href=""http://www.jb51.net"">www.jb51.net</a>",1)) %>