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

Working with strings<br />

var str:String = "The more the merrier.";<br />

// (This search is case-s<strong>en</strong>sitive.)<br />

trace(str.search("the")); // output: 9<br />

You can also use regular expressions to define the pattern to match, as this example shows:<br />

var pattern:RegExp = /the/i;<br />

var str:String = "The more the merrier.";<br />

trace(str.search(pattern)); // 0<br />

The output of the trace() method is 0, because the first character in the string is index position 0. The i flag is set in<br />

the regular expression, so the search is not case-s<strong>en</strong>sitive.<br />

The search() method finds only one match and returns its starting index position, ev<strong>en</strong> if the g (global) flag is set in<br />

the regular expression.<br />

The following example shows a more intricate regular expression, one that matches a string in double quotation marks:<br />

var pattern:RegExp = /"[^"]*"/;<br />

var str:String = "The \"more\" the merrier.";<br />

trace(str.search(pattern)); // output: 4<br />

str = "The \"more the merrier.";<br />

trace(str.search(pattern)); // output: -1<br />

// (Indicates no match, since there is no closing double quotation mark.)<br />

The match() method works similarly. It searches for a matching substring. However, wh<strong>en</strong> you use the global flag in<br />

a regular expression pattern, as in the following example, match() returns an array of matching substrings:<br />

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

var pattern:RegExp = /\w*@\w*\.[org|com]+/g;<br />

var results:Array = str.match(pattern);<br />

The results array is set to the following:<br />

["bob@example.com","omar@example.org"]<br />

For more information on regular expressions, see “Using regular expressions” on page 76.<br />

Replacing matched substrings<br />

You can use the replace() method to search for a specified pattern in a string and replace matches with the specified<br />

replacem<strong>en</strong>t string, as the following example shows:<br />

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

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

trace(str.replace(pattern, "sch")); //sche sells seaschells by the seaschore.<br />

Note that in this example, the matched strings are not case-s<strong>en</strong>sitive because the i (ignoreCase) flag is set in the<br />

regular expression, and multiple matches are replaced because the g (global) flag is set. For more information, see<br />

“Using regular expressions” on page 76.<br />

You can include the following $ replacem<strong>en</strong>t codes in the replacem<strong>en</strong>t string. The replacem<strong>en</strong>t text shown in the<br />

following table is inserted in place of the $ replacem<strong>en</strong>t code:<br />

Last updated 6/6/2012<br />

17

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

Saved successfully!

Ooh no, something went wrong!