Jim
2015-11-04 03:45:23 UTC
I use VBScripts' regex Replace() method a lot to modify strings. It's great for rearranging and removing parts of a string, but I haven't yet found a way, if it's possible, to modify subexpressions found in a match when using Replace().
For example, in the following:
Set re = New RegExp
re.IgnoreCase = True
re.Pattern = "^.*\-\s*(.*)$"
s = "This is a Test - Keep only this part"
WScript.Echo s
s = re.Replace(s, "$1")
WScript.Echo s
I often want to do something like capitalize the matched $1 subexpression. It can be done with Execute() and some more code, but would be convenient if I could figure out how to form the second argument to Replace() so it could be done in just a line of code.
For example, in the following:
Set re = New RegExp
re.IgnoreCase = True
re.Pattern = "^.*\-\s*(.*)$"
s = "This is a Test - Keep only this part"
WScript.Echo s
s = re.Replace(s, "$1")
WScript.Echo s
I often want to do something like capitalize the matched $1 subexpression. It can be done with Execute() and some more code, but would be convenient if I could figure out how to form the second argument to Replace() so it could be done in just a line of code.