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 />

The x (ext<strong>en</strong>ded) flag<br />

Regular expressions can be difficult to read, especially wh<strong>en</strong> they include a lot of metasymbols and metasequ<strong>en</strong>ces. For<br />

example:<br />

/|(\s*[^>]*>)).*?/gi<br />

Wh<strong>en</strong> you use the x (ext<strong>en</strong>ded) flag in a regular expression, any blank spaces that you type in the pattern are ignored.<br />

For example, the following regular expression is id<strong>en</strong>tical to the previous example:<br />

/ |<br />

(\s*<br />

[^>]*<br />

>)) .*? /gix<br />

If you have the x flag set and do want to match a blank space character, precede the blank space with a backslash. For<br />

example, the following two regular expressions are equival<strong>en</strong>t:<br />

/foo bar/<br />

/foo \ bar/x<br />

The lastIndex property<br />

The lastIndex property specifies the index position in the string at which to start the next search. This property<br />

affects the exec() and test() methods called on a regular expression that has the g flag set to true. For example,<br />

consider the following code:<br />

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

var str:String = "Pedro Piper picked a peck of pickled peppers.";<br />

trace(pattern.lastIndex);<br />

var result:Object = pattern.exec(str);<br />

while (result != null)<br />

{<br />

trace(pattern.lastIndex);<br />

result = pattern.exec(str);<br />

}<br />

The lastIndex property is set to 0 by default (to start searches at the beginning of the string). After each match, it is<br />

set to the index position following the match. Therefore, the output for the preceding code is the following:<br />

0<br />

5<br />

11<br />

18<br />

25<br />

36<br />

44<br />

If the global flag is set to false, the exec() and test() methods do not use or set the lastIndex property.<br />

The match(), replace(), and search() methods of the String class start all searches from the beginning of the string,<br />

regardless of the setting of the lastIndex property of the regular expression used in the call to the method. (However,<br />

the match() method does set lastIndex to 0.)<br />

You can set the lastIndex property to adjust the starting position in the string for regular expression matching.<br />

The source property<br />

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

var pattern:RegExp = /foo/gi;<br />

trace(pattern.source); // foo<br />

Last updated 6/6/2012<br />

89

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

Saved successfully!

Ooh no, something went wrong!