VBScript code to replace text NOT in HTML tags
I had been looking for a solution to this problem for a little while and there was a post on ASPMessageboard that got me started. It pointed me to an article on 4GuysFromRolla that included a function as well as some search criteria.
First the function:
Function RegExpReplace(strInput, strPattern, strReplace) ' Use <?> to indicate the match you wish to replace ' Create and setup several variables: Dim regEx, Match, Matches, Position, strReturn Position = 1 strReturn = "" ' Set up the regular expression: Set regEx = New RegExp regEx.Pattern = strPattern regEx.IgnoreCase = True regEx.Global = True ' Get all the matches for it: Set Matches = regEx.Execute(strInput) ' Go through the Matches collection ' and build the output string: For Each Match in Matches strReturn=strReturn & Mid(strInput, Position, Match.FirstIndex+1-Position) strReturn=strReturn & Replace(strReplace, "<?>", Match.Value) Position = Len(Match.Value) + Match.FirstIndex + 1 Next ' Add any text after the last match strReturn = strReturn & Mid(strInput, Position, Len(strInput)) RegExpReplace = strReturn End Function
But the example search criteria in the post didn't quite work for me...
strHTML = RegExpReplace(strHTML, "strong(?![^<]+>), "*<?>*")
...it kept replacing text in html tags as well as text not in html tags. So, I banged around with it and came up with this modification:
strHTML = RegExpReplace(strHTML, "(?![^<]+>)" + strSearch + "(?![^<]+>)", strReplace)
strSearch is the text that I'm looking for and strReplace is what I want to replace it with.
I've done a few quick tests and it seems to be working well.