【问题标题】:vbscript regex get img src URLvbscript 正则表达式获取 img src URL
【发布时间】:2011-07-13 12:42:30
【问题描述】:

我要做的是从下面的 stringXML 获取 IMG SRC URL。 (即http://www.webserver.com/picture.jpg

这是我所拥有的,但它只给我真/假:

<%
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'?

干杯, 画了

【问题讨论】:

    标签: regex vbscript asp-classic


    【解决方案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
    

    【讨论】:

    • 非常感谢!这刚刚解决了数小时的谷歌搜索和试验/错误。再次感谢!!!
    • 一个问题是这也适用于 CSS 和 Script 标签,现在尝试修复它...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2013-11-02
    • 1970-01-01
    相关资源
    最近更新 更多