直接用正則替換,但沒有判斷功能
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.jb51.net/article/15362.htm
下面由指令碼之家測試代碼,大家可以做出函數
<%
content2="<img src='http://www.jb51.net/images/logo.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.jb51.net" 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=""在新視窗中開啟瀏覽"" onclick=""javascript:window.open(this.src);"" onload=""javascript:resizepic(this)"" border=""0""/>")
Next
response.write Content
set re = nothing
%>