Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 305

Programming actionscript 3.0
Table of Contents

Advertisement

Defining the WikiParser class
The WikiParser class includes methods that convert Wiki input text into the equivalent
HTML output. This is not a very robust Wiki conversion application, but it does illustrate
some good uses of regular expressions for pattern matching and string conversion.
The constructor function, along with the
string of Wiki input text, as follows:
public function WikiParser()
{
wikiData = setWikiData();
}
When the user clicks the Test button in the sample application, the application invokes the
parseWikiString()
methods, which in turn assemble the resulting HTML string.
public function parseWikiString(wikiString:String):String
{
var result:String = parseBold(wikiString);
result = parseItalic(result);
result = linesToParagraphs(result);
result = parseBullets(result);
return result;
}
Each of the methods called—
—uses the
parseBullets()
defined by a regular expression, in order to transform the input Wiki text into HTML-
formatted text.
Converting boldface and italic patterns
The
method looks for a Wiki boldface text pattern (such as
parseBold()
transforms it into its HTML equivalent (such as
private function parseBold(input:String):String
{
var pattern:RegExp = /'''(.*?)'''/g;
return input.replace(pattern, "<b>$1</b>");
}
Note that the
(.?*)
between the two defining
for a string such as
'''aaa''' bbb '''ccc'''
and not the entire string (which starts and ends with the
setWikiData()
method of the WikiParser object. This method calls a number of other
parseBold()
method of the string to replace matching patterns,
replace()
portion of the regular expression matches any number of characters (
patterns. The
'''
method, simply initializes a sample
,
,
parseItalic()
linesToParagraphs()
), as follows:
<b>foo</b>
quantifier makes the match nongreedy, so that
?
, the first matched string will be
pattern).
'''
, and
) and
'''foo'''
'''aaa'''
Example: A Wiki parser
)
*
305

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents