User Guide
Regular expression syntax 287
trace(str.match(pattern)); // 337,4, 57, 33
The following methods of the String class take regular expressions as parameters: match(),
replace(), search(), and split(). For more information on these methods, see “Finding
patterns in strings and replacing substrings” on page 216.
The RegExp class includes the following methods:
test() and exec(). For more
information, see “Methods for using regular expressions with strings” on page 303.
Regular expression syntax
This section describes all of the elements of ActionScript regular expression syntax.
Creating an instance of a regular expression
There are two ways to create a regular expression instance. One way uses forward slash
characters (
/) to delineate the regular expression; the other uses the new constructor. For
example, the following regular expressions are equivalent:
var pattern1:RegExp = /bob/i;
var pattern2:RegExp = new RegExp("bob", "i");
Forward slashes delineate a regular expression literal in the same way as quotation marks
delineate a string literal. The part of the regular expression within the forward slashes defines
the pattern. The regular expression can also include flags after the final delineating slash. These
flags are considered to be part of the regular expression, but they are separate from its pattern.
When using the
new constructor, you use two strings to define the regular expression. The
first string defines the pattern, and the second string defines the flags, as in the following
example:
var pattern2:RegExp = new RegExp("bob", "i");
When including a forward slash within a regular expression that is defined by using the
forward slash delineators, you must precede the forward slash with the backslash (
\) escape
character. For example, the following regular expression matches the pattern
1/2:
var pattern:RegExp = /1\/2/;
To include quotation marks within a regular expression that is defined with the new
constructor, you must add backslash (
\) escape character before the quotation marks (just as
you would when defining any String literal). For example, the following regular expressions
match the pattern
eat at "joe's":
var pattern1:RegExp = new RegExp("eat at \"joe's\"", "");
var pattern2:RegExp = new RegExp('eat at "joe\'s"', "");