...

View Full Version : how to multiple search & replace?



Spudhead
03-15-2007, 11:46 AM
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
else
sReplaceURL = sURL & "?c=" & sTrackCode
end if
sRV = replace(sRV, sURL, sReplaceURL)

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

end if

next
set oMatches = nothing
trackLinks = sRV
end function

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

Spudhead
03-16-2007, 04: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum