Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts

    how to multiple search & replace?

    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.

    Code:
    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

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    So that's a no then? Hmmm. This may be harder than I thought.

  • #3
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •