Regex to do some replacing

Posted on 2004-11-24
Last Modified: 2008-03-03
If I have the following in some HTML
<IMG alt="" hspace=0 src="cid:00c801c4a19b$847576f0$6500a8Garyc0" align=baseline border=0>

I need to get this bit i.e. the src of the image

and replace it with for example this

I'm not sure if the src will always be enclosed by " "  but I can't find any that aren't, and this only applies to the img tag but obviously src could be in other tags so it needs to check that it is an img tag.
Question by:Gary
    LVL 2

    Expert Comment

    LVL 58

    Author Comment

    Read the question I asked
    LVL 12

    Expert Comment

    gary, here's something i tested that might work, but it does based on the presumption that the string inside the "src=" is surrounded by double quotes:

    dim regExpTest, testString
    set regExpTest = new regexp
    regExpTest.Pattern = "src=\x22\S+\x22"
    regExpTest.IgnoreCase = true
    regExpTest.Global = true

    testString = "<IMG alt="""" hspace=0 src=""cid:00c801c4a19b$847576f0$6500a8Garyc0"" align=baseline border=0>"
    testString = testString & "<input name=""ok""><IMG src=""fdsgsg345345346534534fs"">"

    testString = regExpTest.Replace(testString, "src=""whatever.jpg""")



    basically this is the reg expression i used:


    where "\x22" represent a double quote
    "\S" = all non-white space character, so i also assume there is no space in the src string.
    LVL 12

    Expert Comment

    >>it needs to check that it is an img tag.

    hm didn't pay attention to that the first time.
    gonna need to modify the reg expression

    LVL 26

    Accepted Solution

    This should do the trick

    Function ReplaceImgSrc(strIn, strNewSrc)
    dim objRX
    set objRX = new RegExp
    with objRX
          .pattern = "<IMG ([^>]*)?src=\x22?([^ >]*)\x22?([^>]*)?>"
          .ignorecase = true
          .global = true
          ReplaceImgSrc = .replace(strIn, "<IMG $1src=""" + strNewSrc + """$3>")
    end with
    set objRX = nothing
    end Function

    'test code
    dim str
    str = "line 1<IMG alt="""" hspace=0 src=""cid:00c801c4a19b$847576f0$6500a8Garyc0"" align=baseline border=0><BR>line2"
    msgbox ReplaceImgSrc(str, "new.jpg")


    LVL 58

    Author Comment

    Seems to work grand for me Hilaire

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now