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

Working with strings<br />

$ Code Replacem<strong>en</strong>t Text<br />

$$ $<br />

$& The matched substring.<br />

$` The portion of the string that precedes the matched substring. This code uses the straight left single quotation<br />

mark character (`), not the straight single quotation mark (') or the left curly single quotation mark (' ).<br />

$' The portion of the string that follows the matched substring. This code uses the straight single quotation mark (' ).<br />

$n The nth captured par<strong>en</strong>thetical group match, where n is a single digit, 1-9, and $n is not followed by a decimal digit.<br />

$nn The nnth captured par<strong>en</strong>thetical group match, where nn is a two-digit decimal number, 01–99. If the nnth capture<br />

is undefined, the replacem<strong>en</strong>t text is an empty string.<br />

For example, the following shows the use of the $2 and $1 replacem<strong>en</strong>t codes, which repres<strong>en</strong>t the first and second<br />

capturing group matched:<br />

var str:String = "flip-flop";<br />

var pattern:RegExp = /(\w+)-(\w+)/g;<br />

trace(str.replace(pattern, "$2-$1")); // flop-flip<br />

You can also use a function as the second parameter of the replace() method. The matching text is replaced by the<br />

returned value of the function.<br />

var str:String = "Now only $9.95!";<br />

var price:RegExp = /\$([\d,]+.\d+)+/i;<br />

trace(str.replace(price, usdToEuro));<br />

function usdToEuro(matchedSubstring:String, capturedMatch1:String, index:int,<br />

str:String):String<br />

{<br />

var usd:String = capturedMatch1;<br />

usd = usd.replace(",", "");<br />

var exchangeRate:Number = 0.853690;<br />

var euro:Number = parseFloat(usd) * exchangeRate;<br />

const euroSymbol:String = String.fromCharCode(8364);<br />

return euro.toFixed(2) + " " + euroSymbol;<br />

}<br />

Wh<strong>en</strong> you use a function as the second parameter of the replace() method, the following argum<strong>en</strong>ts are passed to<br />

the function:<br />

The matching portion of the string.<br />

Any capturing par<strong>en</strong>thetical group matches. The number of argum<strong>en</strong>ts passed this way will vary dep<strong>en</strong>ding on the<br />

number of par<strong>en</strong>thetical matches. You can determine the number of par<strong>en</strong>thetical matches by checking<br />

argum<strong>en</strong>ts.l<strong>en</strong>gth - 3 within the function code.<br />

The index position in the string where the match begins.<br />

The complete string.<br />

Last updated 6/6/2012<br />

18

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

Saved successfully!

Ooh no, something went wrong!