VBScript has built-in support for regular expressions. You can use regular expressions in VBScript by creating one or more instances of the RegExp object which has only four properties and three methods. This object allows you to find regular expression matches in strings, and replace regex matches in strings with other strings.
After creating the object, assign the regular expression you want to search for to the Pattern property. If you want to use a literal regular expression rather than a user-supplied one, simply put the regular expression in a double-quoted string.
By default, the regular expression is case sensitive. Set the IgnoreCase property to True to make it case insensitive.
The caret(^) and dollar($) only match at the very start and very end of the subject string by default. If your subject string consists of multiple lines separated by line breaks, you can make the caret and dollar match at the start and the end of those lines by setting the Multiline property to True. VBScript does not have an option to make the dot match line break characters.
Finally, if you want the RegExp object to return or replace all matches instead of just the first one, set the Global property to True.
'Prepare a regular expression object
Set myRegExp = New RegExp
myRegExp.Pattern = "regex"
myRegExp.IgnoreCase = True
myRegExp.Global = True
After setting the RegExp object's properties, you can invoke one of the three methods to perform one of three basic tasks.
The Test method takes one parameter: a string to test the regular expression on. Test returns True or False, indicating if the regular expression matches (part of) the string. When validating user input, you'll typically want to check if the entire string matches the regular expression. To do so, put a caret at the start of the regex, and a dollar at the end, to anchor the regex at the start and end of the subject string.
The Execute method also takes one string parameter. Instead of returning True or False, it returns a MatchCollection object. If the regex could not match the subject string at all, MatchCollection.Count will be zero. If the RegExp.Global property is False (the default), MatchCollection will contain only the first match. If RegExp.Global is true, Matches> will contain all matches.
The Replace method takes two string parameters. The first parameter is the subject string, while the second parameter is the replacement text. If the RegExp.Global property is False (the default), Replace will return the subject string with the first regex match (if any) substituted with the replacement text. If RegExp.Global is true, Replace will return the subject string with all regex matches replaced.