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

Wh<strong>en</strong> the user clicks the Test button in the sample application, the application invokes the parseWikiString()<br />

method of the WikiParser object. This method calls a number of other methods, which in turn assemble the resulting<br />

HTML string.<br />

public function parseWikiString(wikiString:String):String<br />

{<br />

var result:String = parseBold(wikiString);<br />

result = parseItalic(result);<br />

result = linesToParagraphs(result);<br />

result = parseBullets(result);<br />

return result;<br />

}<br />

Each of the methods called—parseBold(), parseItalic(), linesToParagraphs(), and parseBullets()—uses<br />

the replace() method of the string to replace matching patterns, defined by a regular expression, in order to<br />

transform the input Wiki text into HTML-formatted text.<br />

Converting boldface and italic patterns<br />

The parseBold() method looks for a Wiki boldface text pattern (such as '''foo''') and transforms it into its HTML<br />

equival<strong>en</strong>t (such as foo), as follows:<br />

private function parseBold(input:String):String<br />

{<br />

var pattern:RegExp = /'''(.*?)'''/g;<br />

return input.replace(pattern, "$1");<br />

}<br />

Note that the (.?*) portion of the regular expression matches any number of characters (*) betwe<strong>en</strong> the two defining<br />

''' patterns. The ? quantifier makes the match nongreedy, so that for a string such as '''aaa''' bbb '''ccc''',<br />

the first matched string will be '''aaa''' and not the <strong>en</strong>tire string (which starts and <strong>en</strong>ds with the ''' pattern).<br />

The par<strong>en</strong>theses in the regular expression define a capturing group, and the replace() method refers to this group<br />

by using the $1 code in the replacem<strong>en</strong>t string. The g (global) flag in the regular expression <strong>en</strong>sures that the<br />

replace() method replaces all matches in the string (not simply the first one).<br />

The parseItalic() method works similarly to the parseBold() method, except that it checks for two apostrophes<br />

('') as the delimiter for italic text (not three):<br />

private function parseItalic(input:String):String<br />

{<br />

var pattern:RegExp = /''(.*?)''/g;<br />

return input.replace(pattern, "$1");<br />

}<br />

Converting bullet patterns<br />

As the following example shows, the parseBullet() method looks for the Wiki bullet line pattern (such as * foo)<br />

and transforms it into its HTML equival<strong>en</strong>t (such as foo):<br />

private function parseBullets(input:String):String<br />

{<br />

var pattern:RegExp = /^\*(.*)/gm;<br />

return input.replace(pattern, "$1");<br />

}<br />

The ^ symbol at the beginning of the regular expression matches the beginning of a line. The m (multiline) flag in the<br />

regular expression causes the regular expression to match the ^ symbol against the start of a line, not simply the start<br />

of the string.<br />

Last updated 6/6/2012<br />

92

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

Saved successfully!

Ooh no, something went wrong!