VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

"RegExp" Class and Object for Regular Expression Support

This section describes the 'RegExp' class and objects used for pattern matches using regular expressions. oRegExp.execute(string) returns a MatchCollection collection object.

VBScript supports a built-in class called "RegExp" to provide regular expression support.

In order to perform a regular express task, you need to create a RegExp object with this syntax:

   Set oRegExp = New RegExp

Note that we have to use the "Set" statement to assign an object to a variable.

Then we are ready to use properties and methods supported on the RegExp object, oRegExp:

  • oRegRex.Pattern - A String property containing the regular expression string to represent the search pattern.
  • oRegRex.Global - A Boolean property representing the global search flag. If turned on, the search will be applied multiple times on the entire string.
  • oRegRex.IgnoreCase - A Boolean property representing the ignore-case search flag. If turned on, the search will be applied case insensitively.
  • oRegRex.Test(string) - A method returning True, if a pattern match is found in the given string.
  • oRegRex.Execute(string) - A method returning a MatchCollection collection object that contains a list of Match objects. Each Match object represents a pattern match found in the given string.
  • oRegRex.Replace(string, replacement) - A method returning a copy of the given string with each pattern match found and replaced with the replacement string.

The Match object has the following properties:

  • oMatch.Length - An integer property representing the number of characters of this pattern match.
  • oMatch.FirstIndex - An integer property representing the position where this pattern match was found in the string.
  • oMatch.Value - An String property representing this match found in the string. The "Value" property is the default property. So the "Value" property will be returned if a Match object is used in a String context.
  • oMatch.SubMatches - A SubMatches collection property that contains a list of Strings. Each string represents a sub pattern match.

Notice that MatchCollection and SubMatches objects are collection objects. They share same properties and methods as all collection objects. See the next section for details.

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

Regular Expression Pattern Match and Replacement

"RegExp" Class and Object for Regular Expression Support

 "MatchCollection" and "SubMatches" Collection Objects

 "Set oRegExp = New RegExp" - Creating RegExp Objects

 Example of Regular Expression Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

"RegExp" Class and Object for Regular Expression Support - Updated in 2015, by Dr. Herong Yang