ASP正则获取图片地址

<%
Function RegExp_Execute(string)
Dim regEx, Match, Matches,values '建立变量。
Set regEx = New RegExp '建立正则表达式。
regEx.Pattern = "src\=.+?\.(gif|jpg)" '设置模式。
regEx.IgnoreCase = true '设置是否区分字符大小写。
regEx.Global = True '设置全局可用性。
Set Matches = regEx.Execute(string) '执行搜索。
For Each Match in Matches '遍历匹配集合。
values=values&Match.Value&"""|"
Next
RegExp_Execute = values
End Function
%>

--------------------------------------------------------------------------------------------------

<%
dim result,result1
str="adfjlmnnzlkjlkfjoj <img src=""/article/UploadPic/2009-4/200941873654640.jpg"" border=0 width=100>dfkjhdjfk"
set re=new regexp
re.ignorecase=true
re.global=true
re.pattern="<img [^>]*src=""([^"">]+)""[^>]+>"
set m=re.execute(str)
for each n in m
result=result&n&""
result1=result1&n.submatches(0)&""
next
set m=nothing
set re=nothing
if result<>"" then
result=left(result,len(result)-1)
result1=left(result1,len(result1)-1)
end if
result=split(result,"") '存储<img>
result1=split(result1,"") '存储图像地址
%>

--------------------------------------------------------------------------------------------------

<%

 

'功能:返回字符串,其中指定数目的某子字符串 全部 被替换为另一个子字符串。
'来源:http://jorkin.reallydo.com/article.asp?id=406

Function ReplaceAll(sExpression, sFind, sReplaceWith, bAll)
    
If IsNull(sExpression) Then ReplaceAll = "" : Exit Function
    If
CBool(bAll) Then
        Do While
InStr( 1, sExpression, sFind, 1) > 0
            
sExpression = Replace(sExpression, sFind, sReplaceWith, 1, -1, 1)
            
If InStr( 1, sReplaceWith , sFind , 1) >0 Then Exit Do
        Loop
    Else
        Do While
InStr(sExpression, sFind) > 0
            
sExpression = Replace(sExpression, sFind, sReplaceWith)
            
If InStr(sReplaceWith, sFind ) > 0 Then Exit Do
        Loop
    End If
    
ReplaceAll = sExpression
End Function


'功能:获取全部图片地址,保存到一个数组.
'需要ReplaceAll函数:http://jorkin.reallydo.com/article.asp?id=406
Function getIMG(sString)
Dim sReallyDo, regEx, iReallyDo
Dim oMatches, cMatch
'//定义一个空数组
iReallyDo = -1
ReDim aReallyDo(iReallyDo)
If IsNull(sString) Then
getIMG = ""
Exit Function
End If
'//格式化HTML代码
'//将每个 <img 换行 方便正则替换
sReallyDo = sString
On Error Resume Next
sReallyDo = Replace(sReallyDo, vbCr, " ")
sReallyDo = Replace(sReallyDo, vbLf, " ")
sReallyDo = Replace(sReallyDo, vbTab, " ")
sReallyDo = Replace(sReallyDo, "<img ", vbCrLf & "<img ", 1, -1, 1)
sReallyDo = Replace(sReallyDo, "/>", " />", 1, -1, 1)
sReallyDo = ReplaceAll(sReallyDo, "= ", "=", True)
sReallyDo = ReplaceAll(sReallyDo, "> ", ">", True)
sReallyDo = Replace(sReallyDo, "><", ">" & vbCrLf & "<")
sReallyDo = Trim(sReallyDo)
On Error GoTo 0
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.Global = True
'//去除onclick,onload等脚本
regEx.Pattern = "\s[on].+?=([\""|\'])(.*?)\1"
sReallyDo = regEx.Replace(sReallyDo, "")
'//将SRC不带引号的图片地址加上引号
regEx.Pattern = "<img.*?\ssrc=([^\""\'\s][^\""\'\s>]*).*?>"
sReallyDo = regEx.Replace(sReallyDo, "<img src=""$1"" />")
'//正则匹配图片SRC地址
regEx.Pattern = "<img.*?\ssrc=([\""\'])([^\""\']+?)\1.*?>"
Set oMatches = regEx.Execute(sReallyDo)
'//将图片地址存入数组
For Each cMatch in oMatches
iReallyDo = iReallyDo + 1
ReDim Preserve aReallyDo(iReallyDo)
aReallyDo(iReallyDo) = regEx.Replace(cMatch.Value, "$2")
Next
getIMG = aReallyDo
End Function
%>


posted on 2011-01-20 11:07  韩显川  阅读(319)  评论(0编辑  收藏  举报

导航