当前位置: 首页 > 图文教程 > 网络编程 > 正则表达式 > asp 图片正则 替换,替换前检查图片是不是本地地址的方法

正则表达式
python 正则表达式 反斜杠(/)的麻烦和陷阱
利用正则表达式(只录入中文,数字,英文)
匹配中文的正则(GB2312/utf-8)
JavaScript 实现基础 正则表达式
正则表达式中\w不能识别中文
PHP 正则表达式验证中文的问题
php中utf-8编码下用正则表达式如何匹配汉字
PHP正则匹配图片并给图片加链接详解
PHP 正则表达式的几则使用技巧
PHP 正则表达式常用函数使用小结
常用正则表达式范例 方便表单验证
php 正则 不包含某字符串的正则表达式
用正则表达式表示不包含2950的字符串
用正则删除不包含某个字符串的行的代码
用正则表达式来判断素数的代码
关于表格的正则表达式讨论(表格)
[原创]通过脚本清空标签p中的class名和style
[原创]VBS中的正则表达式的用法大全
去html代码的正则 推荐
正则表达式的应用

正则表达式 中的 asp 图片正则 替换,替换前检查图片是不是本地地址的方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 283 ::
收藏到网摘: n/a

这个图片正则先检查图片的地址,不是本地的则用本地的asp突破盗链,方便使用,注意是答chinaz的朋友问的一个问题 直接用正则替换,但没有判断功能
Function FormatImg(content)
dim re
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern="(script)"
Content=re.Replace(Content,"script")
re.Pattern="<img.[^>]*src(=| )(.[^>]*)>"
Content=re.replace(Content,"<img src=$2 style=""cursor: pointer"" alt=""在新窗口中打开浏览"" onclick=""javascript:window.open(this.src);"" onload=""javascript:resizepic(this)"" border=""0""/>")
set re = nothing
FormatImg = content
End Function
这段代码将内容中的图片替换成 <img src=$2 style="cursor: pointer" alt="在新窗口中打开浏览" onclick="javascript:window.open(this.src);" onload="javascript:resizepic(this)" border="0"/> 这中形式的,
我现在需要提取$2的前7个字符,用来判断是否需要被替换,如果前7=特定的字符,就不要替换,但获取$2的前7 无法。大家有什么办法支下招撒?
主要是参考了下面的代码,大家可以看下
'连接
re.Pattern = "\[url=(.[^\]]*)\](.[^\[]*)\[\/url]"
Set strMatchs=re.Execute(strContent)
For Each strMatch in strMatchs
tmpStr1=checkURL(strMatch.SubMatches(0))
tmpStr2=strMatch.SubMatches(1)
strContent=replace(strContent,strMatch.Value,"<a target=""_blank"" href="""&tmpStr1&""">"&tmpStr2&"</a>",1,-1,0)
Next
这里是正则的Matchs的说明文档
http://www.ruanchen.com/"<img src='/upload/tech/20091012/20091012151530_1f4477bad7af3616c1f933a02bfabe4e.gif' width=100 />中间一些内容<img src='http://www.kanshule.com/indeximg/logo.GIF' width=200 />"
dim re
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern="<img.[^>]*src(=| )(.[^>]*)[/]?>"
set Matches=re.execute(content2)
For Each strMatch in Matches
tmpStr1=(strMatch.SubMatches(1))
tmpurl=replace(replace(tmpStr1,"'",""),"""","")
//response.write left(tmpurl,20)
if left(tmpurl,19)="http://www.ruanchen.com" then
picurl=tmpurl
else
picurl="http://img.ruanchen.com/"&tmpurl
end if
Content=replace(Content2,strMatch.Value,"<img src="&picurl&" style=""cursor: pointer"" alt=""在新窗口中打开浏览"" onclick=""javascript:window.open(this.src);"" onload=""javascript:resizepic(this)"" border=""0""/>")
Next
response.write Content
set re = nothing
%>