User Guide

288 Using Regular Expressions
Do not use the backslash escape character with quotation marks in regular expressions that are
defined by using the forward slash delineators. Similarly, do not use the escape character with
forward slashes in regular expressions that are defined with the
new constructor. The following
regular expressions are equivalent, and they define the pattern
1/2 "joe's":
var pattern1:RegExp = /1\/2 "joe's"/;
var pattern2:RegExp = new RegExp("1/2 \"joe's\"", "");
var pattern3:RegExp = new RegExp('1/2 "joe\'s"', '');
Also, in a regular expression that is defined with the new constructor, to use a metasequence
that begins with the backslash (
\) character, such as \d (which matches any digit), type the
backslash character twice:
var pattern:RegExp = new RegExp("\\d+", ""); // matches one or more digits
You must type the backlash character twice in this case, because the first parameter of the
RegExp() constructor method is a string, and in a string literal you must type a backslash
character twice to have it recognized as a single backslash character.
The sections that follow describe syntax for defining regular expression patterns.
For more information on flags, see “Flags and properties” on page 299.
Characters, metacharacters, and metasequences
The simplest regular expression is one that matches a sequence of characters, as in the
following example:
var pattern:RegExp = /hello/;
However, the following characters, known as metacharacters, have special meanings in regular
expressions:
^ $ \ . * + ? ( ) [ ] { } |
For example, the following regular expression matches the letter A followed by zero or more
instances of the letter B (the asterisk metacharacter indicates this repetition), followed by the
letter C:
/AB*C/
To include a metacharacter without its special meaning in a regular expression pattern, you
must use the backslash (
\) escape character. For example, the following regular expression
matches the letter A followed by the letter B, followed by an asterisk, followed by the letter C:
var pattern:RegExp = /AB\*C/;