To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
Advanced ASPThis forum is for advanced ASP questions. If you have not received a response from the General ASP Q&A forum, try posting your question here.
I am looking for a little help with using asp's regex replace.
I need to replace all ampersands "&" to "amp;", in a multiline string, but only when these ampersands are found in between the quotes of an href.
The following code works for replacing all the ampersands, but it also replaces them on the img src, which is not what I want.
I am not sure if it is my RegEx or my replace function that is my problem, but if anyone can look at my example below, maybe you can guide me on what to change.
function ereg_replace(strOriginalString, strPattern, strReplacement, varIgnoreCase)
' Function replaces pattern with replacement
' varIgnoreCase must be TRUE (match is case insensitive) or FALSE (match is case sensitive)
dim objRegExp : set objRegExp = new RegExp
with objRegExp
.Pattern = strPattern
.IgnoreCase = varIgnoreCase
.Global = True
.Multiline = True
end with
ereg_replace = objRegExp.replace(strOriginalString, strReplacement)
set objRegExp = nothing
end function
Thanks for responding.
You may be right, I may have to look for a different method of attacking this effort.
However, I can't just use the replace function because I will have a large HTML full of hrefs, and everything else. It is not a string I control. It is a mutliline string (HTML) that will be different everytime and I need to parse through and find each href and replace the ampersands.
I am toying with the though of trying to find the position of each "href" and the following double quote and then try to do a replace by the position, but I don't think there is a way.
I am still thinking this should be doable with the RegEx replace. I need to find all X characters, when they appear between A and B substring, and replace the X with Y.
You could use the regex to find a list of matches and then do the replace inside the match.
I know how to (for example) replace a single & inside of a pair of "...", but I don't know a way to replace *all* & without using the replace-in-each-match, as above.
Ok.. so I figure it out- I found a close enough example online. The majority of the work was done by the online contributor, not me.
Basically it is a mix of regex matching, with Position evaluation and Replace.
In my example I replace "&" with "amp;" but only in the hrefs, not the img src calls.
Here is my code example, I hope it helps others.
<%
'this is the multiline string I am using to test
InitialString = "href=""http://video.google.com/videosearch?q=anon+searching&um=1&ie=UTF-8&sa=N&hl=en&ta=wv"">"
InitialString= InitialString & VBCRLF & "img src=""http://images.google.com/image?q=anon+searching&im=1&ie=UTF-8&sa=N&hl=en&ta=wv"">"
InitialString= InitialString & VBCRLF & "href=""http://www.google.com/image?q=anon+searching&um=2&ie=UTF-8&sa=N&hl=en&ta=wv"">"
response.write(RegExpReplace(InitialString, "href=""[^""]*", "amp;"))
Function RegExpReplace(strInput, strPattern, strReplace)
Dim regEx, Match, Matches, Position, strReturn
Position = 1
strReturn = ""
Set regEx = New RegExp
regEx.Pattern = strPattern
regEx.IgnoreCase = True
regEx.Global = True
regEx.Multiline = True
Set Matches = regEx.Execute(strInput)
For Each Match in Matches
' Below line adds everthing from the current position
' to the next pattern match
strReturn = strReturn & Mid(strInput, Position, Match.FirstIndex+1-Position)
' Then this line replaces the ampersand in the matched value from the regex match
' and appends it to the result.
strReturn = strReturn & Replace(Match.Value, "&", strReplace)
Position = Len(Match.Value) + Match.FirstIndex + 1
Next
' Add any text after the last match
strReturn = strReturn & Mid(strInput, Position, Len(strInput))