VBScript String Replace using Regular Expressions
The following describes two methods for using regular expressions to remove or replace characters from a string. The first uses a search patern that finds specific characters to remove or replace, where the second method removes all characters from the input string execpt for any that fall within a given range. Both methods are efficient to use in comparison to the VBScript Replace method to perform the equivelant task.
The following VBScript instantiates a new Regular Expression Object, and sets the search pattern to "[(?*"",~%{}+_.@:/!;]+", which indicates that each of the supplied characters should be removed when the pattern is applied using the replace method of the regular expression object. The regular expression in configured to replace each matching character with a "-". Once complete, a the replace method of the regular expression object is called a second time to remove duplicate "--" characters from the string as a result of the calling the replace method the first time.
Function strClean (strtoclean)
Dim objRegExp, outputStr
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "[(?*"",~%{}+_.@:/!;]+"
outputStr = objRegExp.Replace(strtoclean, "-")
objRegExp.Pattern = "-+"
outputStr = objRegExp.Replace(outputStr, "-")
strClean = outputStr
End Function
The next method uses an "inclusion" method as opposed to the "exclusion" method used above. The search pattern "((?![a-zA-Z0-9]).)+" instead contains the ranges of characters that are allowed in the input string. When the replace method of the regular expression object is called, it replaces all characters in the string except if they fall in one of the ranges supplied in the search pattern.
Function strClean (strtoclean)
Dim objRegExp, outputStr
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "((?![a-zA-Z0-9]).)+"
outputStr = objRegExp.Replace(strtoclean, "-")
objRegExp.Pattern = "-+"
outputStr = objRegExp.Replace(outputStr, "-")
strClean = outputStr
End Function
Similar to the first method, the regular expression is used to process the string by replacing characters from the string that are not within the ranges supplied in the search pattern, then the replace method is called a second time to remove duplicate "--" characters.
Submit a review:
Login required.