Creating a Regular Expression (Scripting) _ Microsoft Learn
Creating a Regular Expression (Scripting) _ Microsoft Learn
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.
/expression/
In Visual Basic Scripting Edition (VBScript), a pair of quotation marks ("") delimit regular
expressions.
"expression"
ノ Expand table
https://learn.microsoft.com/en-us/previous-versions//ee236359(v=vs.85) 1/5
9/18/24, 11:30 AM Creating a Regular Expression (Scripting) | Microsoft Learn
/\d{2}- "\d{2}- Matches an ID number that consists of two digits, a hyphen, and then
\d{5}/ \d{5}" another five digits.
Ordinary Characters
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.
/a/
/7/
/M/
"a"
"7"
"M"
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.
Metacharacters
In addition to ordinary characters, a regular expression pattern can contain
metacharacters. An example of a metacharacter is \d, which matches a digit character.
https://learn.microsoft.com/en-us/previous-versions//ee236359(v=vs.85) 2/5
9/18/24, 11:30 AM Creating a Regular Expression (Scripting) | Microsoft Learn
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.
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".
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".
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.
Quantifiers
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.
https://learn.microsoft.com/en-us/previous-versions//ee236359(v=vs.85) 3/5
9/18/24, 11:30 AM Creating a Regular Expression (Scripting) | Microsoft Learn
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.
ノ Expand table
The following are some sample expressions together with search strings that they
match.
ノ Expand table
"Chapter [1-9][0-9] Matches [0-9] zero or "Chapter 1", "Chapter 25", "Chapter 401320"
{0,}" or more times.
"Chapter [1-9][0-9]*"
"Chapter [0-9]{1,2}" Matches [0-9] one or two "Chapter 0", "Chapter 03", "Chapter 1",
times. "Chapter 25", "Chapter 40"
"Chapter [1-9][0-9] Matches [0-9] zero or "Chapter 1", "Chapter 25", "Chapter 40"
{0,1}" or one time.
"Chapter [1-9][0-9]?"
Specifying Alternatives
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).
Using Submatches
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).
See Also
Concepts
Regular Expression Programming (Scripting)
Change History
ノ Expand table
Added topic.
August 2009 Customer feedback.
https://learn.microsoft.com/en-us/previous-versions//ee236359(v=vs.85) 5/5