13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Using regular expressions<br />

You can use regular expressions with the following methods of the String class: match(), replace(), and search().<br />

For more information on these methods, see “Finding patterns in strings and replacing substrings” on page 16.<br />

Creating an instance of a regular expression<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

There are two ways to create a regular expression instance. One way uses forward slash characters (/) to delineate the<br />

regular expression; the other uses the new constructor. For example, the following regular expressions are equival<strong>en</strong>t:<br />

var pattern1:RegExp = /bob/i;<br />

var pattern2:RegExp = new RegExp("bob", "i");<br />

Forward slashes delineate a regular expression literal in the same way as quotation marks delineate a string literal. The<br />

part of the regular expression within the forward slashes defines the pattern. The regular expression can also include<br />

flags after the final delineating slash. These flags are considered to be part of the regular expression, but they are<br />

separate from its pattern.<br />

Wh<strong>en</strong> using the new constructor, you use two strings to define the regular expression. The first string defines the<br />

pattern, and the second string defines the flags, as in the following example:<br />

var pattern2:RegExp = new RegExp("bob", "i");<br />

Wh<strong>en</strong> including a forward slash within a regular expression that is defined by using the forward slash delineators, you<br />

must precede the forward slash with the backslash (\) escape character. For example, the following regular expression<br />

matches the pattern 1/2:<br />

var pattern:RegExp = /1\/2/;<br />

To include quotation marks within a regular expression that is defined with the new constructor, you must add<br />

backslash (\) escape character before the quotation marks (just as you would wh<strong>en</strong> defining any String literal). For<br />

example, the following regular expressions match the pattern eat at "joe's":<br />

var pattern1:RegExp = new RegExp("eat at \"joe's\"", "");<br />

var pattern2:RegExp = new RegExp('eat at "joe\'s"', "");<br />

Do not use the backslash escape character with quotation marks in regular expressions that are defined by using the<br />

forward slash delineators. Similarly, do not use the escape character with forward slashes in regular expressions that<br />

are defined with the new constructor. The following regular expressions are equival<strong>en</strong>t, and they define the pattern 1/2<br />

"joe's":<br />

var pattern1:RegExp = /1\/2 "joe's"/;<br />

var pattern2:RegExp = new RegExp("1/2 \"joe's\"", "");<br />

var pattern3:RegExp = new RegExp('1/2 "joe\'s"', '');<br />

Also, in a regular expression that is defined with the new constructor, to use a metasequ<strong>en</strong>ce that begins with the<br />

backslash (\) character, such as \d (which matches any digit), type the backslash character twice:<br />

var pattern:RegExp = new RegExp("\\d+", ""); // matches one or more digits<br />

You must type the backlash character twice in this case, because the first parameter of the RegExp() constructor<br />

method is a string, and in a string literal you must type a backslash character twice to have it recognized as a single<br />

backslash character.<br />

The sections that follow describe syntax for defining regular expression patterns.<br />

For more information on flags, see “Flags and properties” on page 87.<br />

Last updated 6/6/2012<br />

78

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!