User Guide

Example: A Wiki parser 305
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
setWikiData() method, simply initializes a sample
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() method of the WikiParser object. This method calls a number of other
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—parseBold(), parseItalic(), linesToParagraphs(), and
parseBullets()—uses the replace() method of the string to replace matching patterns,
defined by a regular expression, in order to transform the input Wiki text into HTML-
formatted text.
Converting boldface and italic patterns
The parseBold() method looks for a Wiki boldface text pattern (such as '''foo''') and
transforms it into its HTML equivalent (such as
<b>foo</b>), as follows:
private function parseBold(input:String):String
{
var pattern:RegExp = /'''(.*?)'''/g;
return input.replace(pattern, "<b>$1</b>");
}
Note that the (.?*) portion of the regular expression matches any number of characters (*)
between the two defining
''' patterns. The ? quantifier makes the match nongreedy, so that
for a string such as
'''aaa''' bbb '''ccc''', the first matched string will be '''aaa'''
and not the entire string (which starts and ends with the
''' pattern).