[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Regex to do some replacing

Posted on 2004-11-24
Medium Priority
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

Expert Comment

ID: 12670710
LVL 58

Author Comment

ID: 12670721
Read the question I asked
LVL 12

Expert Comment

ID: 12670979
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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 12

Expert Comment

ID: 12671157
>>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

Hilaire earned 1400 total points
ID: 12672884
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

ID: 12674160
Seems to work grand for me Hilaire

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
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…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

873 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