关于HTML取图片地址的正则表达式

function GetImgSrc(str) '取得img 标签内容
dim tmp,objRegExp,Matches,Match
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True '忽略大小写
objRegExp.Global = false '全文搜索 !关键!
objRegExp.Pattern = "<img (.*?)src=(.[^\[^>]*)(.*?)>"
Set Matches =objRegExp.Execute(str)
For Each Match in Matches
tmp=tmp & Match.Value
Next
GetImgSrc=getimgs(tmp)
end function

function getimgs(str)'取得
dim objRegExp1,mm,Match1,imgsrc
Set objRegExp1 = New Regexp
objRegExp1.IgnoreCase = True '忽略大小写
objRegExp1.Global = True '全文搜索
objRegExp1.Pattern = "src=.+[(gif|jpg|png)]+" '表达式
set mm=objRegExp1.Execute(str)
For Each Match1 in mm
imgsrc=Match1.Value
'也许存在不能过滤的字符,确保万一
imgsrc=replace(imgsrc,"""","")
imgsrc=replace(imgsrc,"src=","")
imgsrc=replace(imgsrc,"<","")
imgsrc=replace(imgsrc,">","")
imgsrc=replace(imgsrc,"img","")
imgsrc=replace(imgsrc," ","")
getimgs=getimgs&imgsrc'把里面的地址串起来备用
next
end function

上面的代码.有点不足.就是取图片地址的时候,把后面的字符串也链接起来
如:正确的地址是:
http://xxx.com/images/000.jpg


取出的时候却为:
http://xxx.com/images/000.jpg
dfdsfw

后缀名多了一些字符.,高手们,这正则表达式能不能再完善点
最新回答
劫后余生

2025-02-25 06:38:29

用第一个函数就行了,修改一下

Function GetImgSrc(str) '取得img 标签内容
Dim tmp, objRegExp, Matches, Match
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True '忽略大小写
objRegExp.Global = True '全文搜索 !关键!
objRegExp.Pattern = "<img [^>]*?\bsrc=(['""]?)([^'""\x20]+)\1 [^>]*?>"
Set Matches = objRegExp.Execute(str)
For Each Match In Matches
tmp = tmp & Match.SubMatches(1) & vbCrLf
Next
GetImgSrc = tmp
End Function