Creating a Regular Expression (Scripting)

Updated: January 2010

A regular expression describes one or more strings to match when you search a body of text. The regular expression serves as a character pattern to compare with the text that is being searched.

Regular expressions are constructed much like arithmetic expressions are created. Small expressions are combined by using a variety of metacharacters and operators to create larger expressions.

The components of a regular expression can be individual characters, sets of characters, ranges of characters, or choices between characters. Components can also be any combination of these components.

You construct a regular expression by putting the various components of the expression between a pair of delimiters. In JScript, the delimiters are a pair of forward slash (/) characters, as shown in the following example.


In Visual Basic Scripting Edition (VBScript), a pair of quotation marks ("") delimit regular expressions.


The following table contains examples of typical regular expressions.




/^\s[ \t]*$/


Matches a blank line.



Matches an ID number that consists of two digits, a hyphen, and then another five digits.

The simplest form of a regular expression is a single, ordinary character that is compared with a searched string. For example, the single-character regular expression "A" matches the letter A wherever it appears in the searched string.

Following are some examples of JScript single-character regular expressions.


The equivalent VBScript single-character regular expressions are as follows.


You can combine several single characters to form a longer expression. For example, the expression /the/ matches "the" in the following searched strings: "the", "there", "other", and "over the lazy dog".

No concatenation operator is needed. All that is required is that you put one character after another.

In addition to ordinary characters, a regular expression pattern can contain metacharacters. An example of a metacharacter is \d, which matches a digit character.

See Regular Expression Syntax (Scripting) for more information.

Ordinary characters consist of all printable and non-printable characters that are not explicitly designated as metacharacters. This includes all uppercase and lowercase alphabetic characters, all digits, all punctuation marks, and some symbols.

The period character (.) matches any single printing or non-printing character in a string, except the newline character (\n). The regular expression /a.c/ in JScript or "a.c" in VBScript matches "aac", "abc", "acc", "adc", "a1c", "a2c", "a-c", and "a#c".

To match a period (.) that is contained in the searched string, you can precede the period in the regular expression with a backslash (\) character. The regular expression /filename\.ext/ in JScript or "filename\.ext" in VBScript matches "filename.ext".

You can create a list of matching characters by enclosing one or more individual characters in brackets [ ].

Any character enclosed in a bracket expression matches only a single character in the position in the regular expression where the bracket expression appears. For example, the /Chapter [12345]/ expression in JScript matches "Chapter 1", "Chapter 2", "Chapter 3", "Chapter 4", and "Chapter 5".

To express the matching characters by using a range instead of the characters themselves, you can use the hyphen (-) character. The /Chapter [1-5]/ expression in JScript is equivalent to /Chapter [12345]/.

You can find all the characters that are not in the list or range by including a caret (^) character at the start of the list. For example, the /[^aAeEiIoOuU]/ expression in JScript matches any non-vowel character.

For more information, see Lists of Matching Characters (Scripting).

You can use quantifiers to specify a regular expression in which a character or set of characters is repeated a specific number of times.

A quantifier refers to the expression immediately previous (to the left of) the quantifier.

Quantifiers are enclosed in braces {}, and include number values for the lower and upper occurrence limits. For example, c{1,2} matches 1 or 2 occurrences of the letter c.

When only one number is specified, it is used as the upper bound unless it is followed by a comma. For example, c{3} matches exactly 3 characters of the letter c, and c{5,} matches 5 or more occurrences of the letter c.

Single-character quantifiers are also available, as shown in the following table.


Explicit quantifier




Matches the previous element zero or more times.



Matches the previous element one or more times.



Matches the previous element zero or one time.

The following are some sample expressions together with search strings that they match.

Expression in VBScript

Meaning of quantifier


"Chapter [1-9][0-9]{0,}" or

"Chapter [1-9][0-9]*"

Matches [0-9] zero or more times.

"Chapter 1", "Chapter 25", "Chapter 401320"

"Chapter [0-9]{1,2}"

Matches [0-9] one or two times.

"Chapter 0", "Chapter 03", "Chapter 1", "Chapter 25", "Chapter 40"

"Chapter [1-9][0-9]{0,1}" or

"Chapter [1-9][0-9]?"

Matches [0-9] zero or one time.

"Chapter 1", "Chapter 25", "Chapter 40"

For more information, see Quantifiers (Scripting).

Anchors enable you to specify that a regular expression must appear either at the start or end of the searched string, or the start or end of a line or word in the searched string, to constitute a match. For more information, see Anchors (Scripting).

The "|" character specifies that two or more alternatives represent a match. For example, the JScript regular expression /(Chapter|Section) [1-9]/ matches the following: "Chapter 1", "Chapter 9", and "Section 2". For more information, see Alternation and Subexpressions (Scripting).

Parentheses in a regular expression are used to create a subexpression. The resulting submatch can be retrieved by the program. For more information, see Alternation and Subexpressions (Scripting).

You can refer to a subexpression from within a regular expression, and from within a replacement string. For more information, see Backreferences (Scripting).




January 2010

Modified "Using Submatches" section.

Information enhancement.

August 2009

Added topic.

Customer feedback.

Community Additions