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

Using regular expressions<br />

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

trace(str.search(/sh/)); // output: 13 -- Not the first character<br />

With the i flag set, however, the regular expression does match the capital letter S:<br />

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

trace(str.search(/sh/i)); // output: 0<br />

The i flag ignores case s<strong>en</strong>sitivity only for the A–Z and a–z characters, but not for ext<strong>en</strong>ded characters such as É and é .<br />

The m (multiline) flag<br />

If the m (multiline) flag is not set, the ^ matches the beginning of the string and the $ matches the <strong>en</strong>d of the string.<br />

If the m flag is set, these characters match the beginning of a line and <strong>en</strong>d of a line, respectively. Consider the following<br />

string, which includes a newline character:<br />

var str:String = "Test\n";<br />

str += "Multiline";<br />

trace(str.match(/^\w*/g)); // Match a word at the beginning of the string.<br />

Ev<strong>en</strong> though the g (global) flag is set in the regular expression, the match() method matches only one substring, since<br />

there is only one match for the ^—the beginning of the string. The output is:<br />

Test<br />

Here is the same code with the m flag set:<br />

var str:String = "Test\n";<br />

str += "Multiline";<br />

trace(str.match(/^\w*/gm)); // Match a word at the beginning of lines.<br />

This time, the output includes the words at the beginning of both lines:<br />

Test,Multiline<br />

Note that only the \n character signals the <strong>en</strong>d of a line. The following characters do not:<br />

Return (\r) character<br />

Unicode line-separator (\u2028) character<br />

Unicode paragraph-separator (\u2029) character<br />

The s (dotall) flag<br />

If the s (dotall or “dot all”) flag is not set, a dot (.) in a regular expression pattern does not match a newline character<br />

(\n). So for the following example, there is no match:<br />

var str:String = "Test\n";<br />

str += "Multiline";<br />

var re:RegExp = /.*?/;<br />

trace(str.match(re));<br />

However, if the s flag is set, the dot matches the newline character:<br />

var str:String = "Test\n";<br />

str += "Multiline";<br />

var re:RegExp = /.*?/s;<br />

trace(str.match(re));<br />

In this case, the match is the <strong>en</strong>tire substring within the tags, including the newline character:<br />

Test<br />

Multiline<br />

Last updated 6/6/2012<br />

88

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

Saved successfully!

Ooh no, something went wrong!