View Full Version : how to multiple search & replace?

03-15-2007, 12:46 PM
I have an intranet feature. You upload a HTML email template, and it goes thorough the code for you. What it does to the code varies, but mainly it's finding all the HREFs and adding some tracking code to them. This might involve adding a querystring value to the original URL, or rewriting the URL so it goes to another site, with the original URL as a querystring value. But basically it's: find all the HREFs and either prefix or suffix them.

So, say I have three links on my HTML email that go to www.google.com - my intranet function will find them using the following RegExp:

oRegEx.Pattern = "<a\s+href=""(http://[^""]*)"".*?>"

It will then loop through all these matches, replacing the original link with a prefixed or suffixed version.

Trouble is, the code I have is a little over-zealous. As the replaced URL still contains the original URL, when the script comes to a subsequent instance of that URL (and the same link might pop up several times in one email), it'll replace ALL instances of it, regardless of whether or not it's already been prefixed or suffixed.

I suspect that this is less of a complex technical issue than my post may imply, and that the root of the problem lies in my own inability to think in joined-up. But I'd appreciate any help. My current code is below, for what it's worth.

function trackLinks(sEmailSourceCode, sClientWebURL, sType)

dim oRegEx, oMatches, oMatch, sRV, sURL, sReplaceURL, sTrackCode
if sType="email" then
sTrackCode = "[*TRACKING*]CODE[/*TRACKING*]"
elseif sType="browser" then
sTrackCode = "<" 'split or the ASP parser tries to read it...
sTrackCode = sTrackCode & "%=request.querystring(""c"")%"
sTrackCode = sTrackCode & ">"
end if
sRV = sEmailSourceCode
set oRegEx = New RegExp
oRegEx.Global = True
oRegEx.IgnoreCase = False
oRegEx.Pattern = "<a\s+href=""(http://[^""]*)"".*?>"
set oMatches = oRegEx.Execute(sEmailSourceCode)
set oRegEx = nothing
for each oMatch in oMatches
sURL = oMatch.SubMatches(0)

if not instr(sURL,"unsubscribe") and not instr(sURL,"[*VIEWBROWSER*]") and not inStr(sURL, "[*TRACKING*]CODE[/*TRACKING*]") then

if inStr(sURL, sClientWebURL) and sClientWebURL <> "" then
if inStr(sURL, "?") then
sReplaceURL = sURL & "&c=" & sTrackCode
sReplaceURL = sURL & "?c=" & sTrackCode
end if
sRV = replace(sRV, sURL, sReplaceURL)

sReplaceURL = "http://emailtracking.mycompany.com/tracking.asp?c=" & sTrackCode & "&r=" & sURL
sRV = replace(sRV, sURL, sReplaceURL)
end if

end if

set oMatches = nothing
trackLinks = sRV
end function

03-16-2007, 02:17 PM
So that's a no then? Hmmm. This may be harder than I thought.

03-16-2007, 05:57 PM
Ok. Let me put it shorter:

How do you stop a regExp-powered replace function replacing previously matched instances when you use it in combination with replace()?

I can't count characters because the character count will increase - and not necessarily by a predictable amount - when the strings are replaced.