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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Using regular expressions<br />

Flags and properties<br />

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

The following table lists the five flags that you can set for regular expressions. Each flag can be accessed as a property<br />

of the regular expression object.<br />

Flag Property Description<br />

g global Matches more than one match.<br />

i ignoreCase Case-ins<strong>en</strong>sitive matching. Applies to the A—Z and a—z characters, but not to ext<strong>en</strong>ded characters<br />

such as É and é .<br />

m multiline With this flag set, $ and ^ can match the beginning of a line and <strong>en</strong>d of a line, respectively.<br />

s dotall With this flag set, . (dot) can match the newline character (\n).<br />

x ext<strong>en</strong>ded Allows ext<strong>en</strong>ded regular expressions. You can type spaces in the regular expression, which are ignored<br />

as part of the pattern. This lets you type regular expression code more legibly.<br />

Note that these properties are read-only. You can set the flags (g, i, m, s, x) wh<strong>en</strong> you set a regular expression variable,<br />

as follows:<br />

var re:RegExp = /abc/gimsx;<br />

However, you cannot directly set the named properties. For instance, the following code results in an error:<br />

var re:RegExp = /abc/;<br />

re.global = true; // This g<strong>en</strong>erates an error.<br />

By default, unless you specify them in the regular expression declaration, the flags are not set, and the corresponding<br />

properties are also set to false.<br />

Additionally, there are two other properties of a regular expression:<br />

The lastIndex property specifies the index position in the string to use for the next call to the exec() or test()<br />

method of a regular expression.<br />

The source property specifies the string that defines the pattern portion of the regular expression.<br />

The g (global) flag<br />

Wh<strong>en</strong> the g (global) flag is not included, a regular expression matches no more than one match. For example, with<br />

the g flag not included in the regular expression, the String.match() method returns only one matching substring:<br />

var str:String = "she sells seashells by the seashore.";<br />

var pattern:RegExp = /sh\w*/;<br />

trace(str.match(pattern)) // output: she<br />

Wh<strong>en</strong> the g flag is set, the Sting.match() method returns multiple matches, as follows:<br />

var str:String = "she sells seashells by the seashore.";<br />

var pattern:RegExp = /sh\w*/g;<br />

// The same pattern, but this time the g flag IS set.<br />

trace(str.match(pattern)); // output: she,shells,shore<br />

The i (ignoreCase) flag<br />

By default, regular expression matches are case-s<strong>en</strong>sitive. Wh<strong>en</strong> you set the i (ignoreCase) flag, case s<strong>en</strong>sitivity is<br />

ignored. For example, the lowercase s in the regular expression does not match the uppercase letter S, the first<br />

character of the string:<br />

Last updated 6/6/2012<br />

87

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

Saved successfully!

Ooh no, something went wrong!