User Guide

Regular expression syntax 295
Alternation
Use the | (bar) character in a regular expression to have the regular expression engine consider
alternatives for a match. For example, the following regular expression matches any one of the
words
cat, dog, pig, rat:
var pattern:RegExp = /cat|dog|pig|rat/;
You can use parentheses to define groups to restrict the scope of the | alternator. The following
regular expression matches
cat followed by nap or nip:
var pattern:RegExp = /cat(nap|nip)/;
For more information, see “Groups” on page 295.
The following two regular expressions, one using the
| alternator, the other using a character
class (defined with
[ and ] ), are equivalent:
/1|3|5|7|9/
/[13579]/
For more information, see “Character classes” on page 291.
Groups
You can specify a group in a regular expression by using parentheses, as follows:
/class-(\d*)/
A group is a subsection of a pattern. You can use groups to do the following things:
Apply a quantifier to more than one character.
Delineate subpatterns to be applied with alternation (by using the | character).
Capture substring matches (for example, by using \1 in a regular expression to match a
previously matched group, or by using
$1 similarly in the replace() method of the
String class).
The following sections provide details on these uses of groups.