当前位置: 移动技术网 > IT编程>开发语言>正则 > ASP超级链接和HTML函数正则表达式 修正版

ASP超级链接和HTML函数正则表达式 修正版

2017年12月12日  | 移动技术网IT编程  | 我要评论
过滤超级链接
复制代码 代码如下:

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 = "(\r|\n|\r\n| |\t| )"
set matches = objregexp.execute(strhtml)
for each match in matches
strhtml=replace(strhtml,match.value,"")
next
objregexp.pattern = "(\r|\n|\r\n| |\t| )"
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

复制代码 代码如下:

<js jsid="1" jsname="去除内容页里干扰信息"><![cdata[
var sourcehtml=function(){/*%s*/}.tostring().slice(13, -3);
parser9527=function()
{
var ss=sourcehtml;
ss=ss.replace(/<.*?>/ig,"");
ss=ss.replace(/(\r|\n|\r\n| |\t| )/ig,"");
ss=ss.replace(/(<\/a>)/ig,"");
ss=ss.replace(/<a((.|\n)*?)>/ig,"");
ss=ss.replace("_网易新闻中心","");
/*
ss=ss.replace(/<iframe((.|\n)*?)<\/iframe>/ig,"");
ss=ss.replace(/<script((.|\n)*?)<\/script>/ig,"");
ss=ss.replace(/<address((.|\n)*?)<\/address>/ig,"");
ss=ss.replace(/(<\/a>)/ig,"");
ss=ss.replace(/<a((.|\n)*?)>/ig,"");
ss=ss.replace(/<select((.|\n)*?)<\/select>/ig,"");
ss=ss.replace(/<table((.|\n)*?)<\/table>/ig,"");
ss=ss.replace(/<img((.|\n)*?)>/ig,"");
ss=ss.replace(/<table((.|\n)*?)>/ig,"");
ss=ss.replace(/(<\/table>)/ig,"");
ss=ss.replace(/<tr((.|\n)*?)>/ig,"<br>");
ss=ss.replace(/(<\/tr>)/ig,"");
ss=ss.replace(/<nobr((.|\n)*?)>/ig,"<br>");
ss=ss.replace(/<nobr((.|\n)*?)>/ig,"");
ss=ss.replace(/(<\/nobr>)/ig,"");
ss=ss.replace(/<td((.|\n)*?)>/ig," ");
ss=ss.replace(/(<\/td>)/ig,"");
ss=ss.replace(/<span id=((.|\n)*?)<\/small>/ig,"");
ss=ss.replace(/<div((.|\n)*?)>/ig,"");
ss=ss.replace(/(<\/div>)/ig,"");
*/
return ss;
}
parser9527();
]]></js>

asp常用的正则过滤函数 可过滤html js style div font

开发程序,经常要用到正则表达式进行过滤一些不需要的东西,比如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.xxx.com 替换成 <a href="//www.jb51.net">www.jb51.net</a>
response.write(replacereg("移动技术网www.xxx.com","www\.xxx\.com","<a href=""//www.jb51.net"">www.jb51.net</a>",1))
%>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网