2011-07-13 48 views
0

我想要做的是從下面的stringXML獲取IMG SRC URL。 (即http://www.webserver.com/picture.jpgvbscript正則表達式獲取img src網址

這是我有什麼,但它僅給我的true/false:

<% 
stringXML="<a href="http://www.webserver.com/"><img src="http://www.webserver.com/picture.jpg"/></a><br>Some text here, blah blah blah." 

Dim objRegex 
    Set objRegex = New Regexp 
     With objRegEx 
      .IgnoreCase = True 
      .Global = True 
      .Multiline = True 
    End with 

strRegexPattern = "\<img\s[^\>]*?src=[""'][^\>]*?(jpg|bmp|gif)[""']" 


objRegEx.Pattern = strRegexPattern 

     response.write objRegEx.Test(stringXML) 

If objRegEx.Test(stringXML) = True Then 
    'The string has a tags. 

    'Match all A Tags 
    Set objRegExMatch = objRegEx.Execute(stringXML) 

    If objRegExMatch.Count > 0 Then 
     Redim arrAnchor(objRegExMatch.Count - 1) 
     For Each objRegExMatchItem In objRegExMatch 
      response.write objRegExMatchItem.Value 
     Next 
    End If 
End If 
%> 

我基本上只想得到IMG SRC值..

任何想法,爲什麼這行不工作'response.write objRegExMatchItem.Value'?

乾杯, 德魯

回答

1

嘗試:

Function getImgTagURL(HTMLstring) 
    Set RegEx = New RegExp 
    With RegEx 
     .Pattern = "src=[\""\']([^\""\']+)" 
     .IgnoreCase = True 
     .Global = True 
    End With 

    Set Matches = RegEx.Execute(HTMLstring) 
    'Iterate through the Matches collection. 
    URL = "" 
    For Each Match in Matches 
     'We only want the first match. 
     URL = Match.Value 
     Exit For 
    Next 
    'Clean up 
    Set Match = Nothing 
    Set RegEx = Nothing 
    ' src=" is hanging on the front, so we will replace it with nothing 
    getImgTagURL = Replace(URL, "src=""", "") 
End Function 
+0

謝謝SOOOO多!這只是解決了谷歌搜索和試用/錯誤的幾個小時。再次感謝!!! – Drew

+0

一個問題是,這也適用於CSS和腳本標籤,現在試圖解決這個問題... –