User Guide
306 Using Regular Expressions
The parentheses in the regular expression define a capturing group, and the replace()
method refers to this group by using the
$1 code in the replacement string. The g (global)
flag in the regular expression ensures that the
replace() method replaces all matches in the
string (not simply the first one).
The
parseItalic() method works similarly to the parseBold() method, except that it
checks for two apostrophes (
'') as the delimiter for italic text (not three):
private function parseItalic(input:String):String
{
var pattern:RegExp = /''(.*?)''/g;
return input.replace(pattern, "<i>$1</i>");
}
Converting bullet patterns
As the following example shows, the parseBullet() method looks for the Wiki bullet line
pattern (such as
* foo) and transforms it into its HTML equivalent (such as <li>foo</li>):
private function parseBullets(input:String):String
{
var pattern:RegExp = /^\*(.*)/gm;
return input.replace(pattern, "<li>$1</li>");
}
The ^ symbol at the beginning of the regular expression matches the beginning of a line. The
m (multiline) flag in the regular expression causes the regular expression to match the ^
symbol against the start of a line, not simply the start of the string.
The
\* pattern matches an asterisk character (the backslash is used to signal a literal asterisk
instead of a
* quantifier).
The parentheses in the regular expression define a capturing group, and the
replace()
method refers to this group by using the
$1 code in the replacement string. The g (global)
flag in the regular expression ensures that the
replace() method replaces all matches in the
string (not simply the first one).
Converting paragraph Wiki patterns
The linesToParagraphs() method converts each line in the input Wiki string to an HTML
<p> paragraph tag. These lines in the method strip out empty lines from the input Wiki
string:
var pattern:RegExp = /^$/gm;
var result:String = input.replace(pattern, "");