asp 圖片正則 替換,替換前檢查圖片是不是本地地址的方法
更新時(shí)間:2008年08月03日 15:55:41 作者:
這個(gè)圖片正則先檢查圖片的地址,不是本地的則用本地的asp突破盜鏈,方便使用,注意是答chinaz的朋友問(wèn)的一個(gè)問(wèn)題
直接用正則替換,但沒(méi)有判斷功能
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=""在新窗口中打開(kāi)瀏覽"" onclick=""javascript:window.open(this.src);"" onload=""javascript:resizepic(this)"" border=""0""/>")
set re = nothing
FormatImg = content
End Function
這段代碼將內(nèi)容中的圖片替換成 <img src=$2 style="cursor: pointer" alt="在新窗口中打開(kāi)瀏覽" onclick="javascript:window.open(this.src);" onload="javascript:resizepic(this)" border="0"/> 這中形式的,
我現(xiàn)在需要提取$2的前7個(gè)字符,用來(lái)判斷是否需要被替換,如果前7=特定的字符,就不要替換,但獲取$2的前7 無(wú)法。大家有什么辦法支下招撒?
主要是參考了下面的代碼,大家可以看下
'連接
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的說(shuō)明文檔
http://www.dbjr.com.cn/article/15362.htm
下面由腳本之家測(cè)試代碼,大家可以做出函數(shù)
<%
content2="<img src='http://www.dbjr.com.cn/images/logo.gif' width=100 />中間一些內(nèi)容<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.dbjr.com.cn" then
picurl=tmpurl
else
picurl="http://img.jb51.net/showpic.asp?url="&tmpurl
end if
Content=replace(Content2,strMatch.Value,"<img src="&picurl&" style=""cursor: pointer"" alt=""在新窗口中打開(kāi)瀏覽"" onclick=""javascript:window.open(this.src);"" onload=""javascript:resizepic(this)"" border=""0""/>")
Next
response.write Content
set re = nothing
%>
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=""在新窗口中打開(kāi)瀏覽"" onclick=""javascript:window.open(this.src);"" onload=""javascript:resizepic(this)"" border=""0""/>")
set re = nothing
FormatImg = content
End Function
這段代碼將內(nèi)容中的圖片替換成 <img src=$2 style="cursor: pointer" alt="在新窗口中打開(kāi)瀏覽" onclick="javascript:window.open(this.src);" onload="javascript:resizepic(this)" border="0"/> 這中形式的,
我現(xiàn)在需要提取$2的前7個(gè)字符,用來(lái)判斷是否需要被替換,如果前7=特定的字符,就不要替換,但獲取$2的前7 無(wú)法。大家有什么辦法支下招撒?
主要是參考了下面的代碼,大家可以看下
'連接
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的說(shuō)明文檔
http://www.dbjr.com.cn/article/15362.htm
下面由腳本之家測(cè)試代碼,大家可以做出函數(shù)
<%
content2="<img src='http://www.dbjr.com.cn/images/logo.gif' width=100 />中間一些內(nèi)容<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.dbjr.com.cn" then
picurl=tmpurl
else
picurl="http://img.jb51.net/showpic.asp?url="&tmpurl
end if
Content=replace(Content2,strMatch.Value,"<img src="&picurl&" style=""cursor: pointer"" alt=""在新窗口中打開(kāi)瀏覽"" onclick=""javascript:window.open(this.src);"" onload=""javascript:resizepic(this)"" border=""0""/>")
Next
response.write Content
set re = nothing
%>
相關(guān)文章
自己學(xué)正則時(shí)做的筆記,其實(shí)正則也不難哦 php
自己學(xué)正則時(shí)做的筆記,其實(shí)正則也不難哦 php...2006-12-12一文搞懂正則表達(dá)式基礎(chǔ)語(yǔ)法以及如何應(yīng)用
正則表達(dá)式是一種描述字符串結(jié)構(gòu)的語(yǔ)法規(guī)則,是用于匹配字符串中字符組合的模式,同時(shí)正則表達(dá)式也是對(duì)象,下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)一文搞懂正則表達(dá)式基礎(chǔ)語(yǔ)法以及如何應(yīng)用的相關(guān)資料,需要的朋友可以參考下2022-09-09python3爬蟲(chóng)之入門(mén)基礎(chǔ)和正則表達(dá)式
這篇文章主要介紹了python3爬蟲(chóng)之入門(mén)基礎(chǔ)和正則表達(dá)式的相關(guān)資料,需要的朋友可以參考下2015-10-10