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

var pattern:RegExp = /(\w+)@(\w+).(com|org)/;<br />

var str:String = "bob@example.com";<br />

trace(pattern.exec(str));<br />

// bob@example.com,bob,example,com<br />

//noncapturing:<br />

var pattern:RegExp = /(\w+)@(\w+).(?:com|org)/;<br />

var str:String = "bob@example.com";<br />

trace(pattern.exec(str));<br />

// bob@example.com,bob,example<br />

A special type of noncapturing group is the lookahead group, of which there are two types: the positive lookahead group<br />

and the negative lookahead group.<br />

Use (?= and ) to define a positive lookahead group, which specifies that the subpattern in the group must match at the<br />

position. However, the portion of the string that matches the positive lookahead group can match remaining patterns<br />

in the regular expression. For example, because (?=e) is a positive lookahead group in the following code, the<br />

character e that it matches can be matched by a subsequ<strong>en</strong>t part of the regular expression—in this case, the capturing<br />

group, \w*):<br />

var pattern:RegExp = /sh(?=e)(\w*)/i;<br />

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

trace(pattern.exec(str));<br />

// Shelly,elly<br />

Use (?! and ) to define a negative lookahead group that specifies that the subpattern in the group must not match at<br />

the position. For example:<br />

var pattern:RegExp = /sh(?!e)(\w*)/i;<br />

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

trace(pattern.exec(str));<br />

// shore,ore<br />

Using named groups<br />

A named group is a type of group in a regular expression that is giv<strong>en</strong> a named id<strong>en</strong>tifier. Use (?P and ) to<br />

define the named group. For example, the following regular expression includes a named group with the id<strong>en</strong>tifier<br />

named digits:<br />

var pattern = /[a-z]+(?P\d+)[a-z]+/;<br />

Wh<strong>en</strong> you use the exec() method, a matching named group is added as a property of the result array:<br />

var myPattern:RegExp = /([a-z]+)(?P\d+)[a-z]+/;<br />

var str:String = "a123bcd";<br />

var result:Array = myPattern.exec(str);<br />

trace(result.digits); // 123<br />

Here is another example, which uses two named groups, with the id<strong>en</strong>tifiers name and dom:<br />

var emailPattern:RegExp =<br />

/(?P(\w|[_.\-])+)@(?P((\w|-)+))+\.\w{2,4}+/;<br />

var address:String = "bob@example.com";<br />

var result:Array = emailPattern.exec(address);<br />

trace(result.name); // bob<br />

trace(result.dom); // example<br />

Note: Named groups are not part of the ECMAScript language specification. They are an added feature in ActionScript 3.0.<br />

Last updated 6/6/2012<br />

86

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

Saved successfully!

Ooh no, something went wrong!