User Guide
Example: A Wiki parser 307
The ^ and $ symbols the regular expression match the beginning and end 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
replace() method replaces all matching substrings (empty lines) with an empty 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 URLs to HTML <a> tags
When the user clicks the Test button in the sample application, if the user selected the
urlToATag check box, the application calls the URLParser.urlToATag() static method to
convert URL strings from the input Wiki string into HTML
<a> tags.
var protocol:String = "((?:http|ftp)://)";
var urlPart:String = "([a-z0-9_-]+\.[a-z0-9_-]+)";
var optionalUrlPart:String = "(\.[a-z0-9_-]*)";
var urlPattern:RegExp = new RegExp(protocol + urlPart + optionalUrlPart,
"ig");
var result:String = input.replace(urlPattern,
"<a href='$1$2$3'><u>$1$2$3</u></a>");
The RegExp() constructor function is used to assemble a regular expression (urlPattern)
from a number of constituent parts. These constituent parts are each strings that define part
of the regular expression pattern.
The first part of the regular expression pattern, defined by the
protocol string, defines an
URL protocol: either
http:// or ftp://. The parentheses define a noncapturing group,
indicated by the
? symbol. This means that the parentheses are simply used to define a group
for the
| alternation pattern; the group will not match backreference codes ($1, $2, $3) in the
replacement string of the
replace() method.
The other constituent parts of the regular expression each use capturing groups (indicated by
parentheses in the pattern), which are then used in the backreference codes (
$1, $2, $3) in the
replacement string of the
replace() method.
The part of the pattern defined by the
urlPart string matches at least one of the following
characters:
a-z, 0-9, _, or -. The + quantifier indicates that at least one character is matched.
The
\. indicates a required dot (.) character. And the remainder matches another string of at
least one of these characters:
a-z, 0-9, _, or -.
The part of the pattern defined by the
optionalUrlPart string matches zero or more of the
following: a dot (
.) character followed by any number of alphanumeric characters (including
_ and -). The * quantifier indicates that zero or more characters are matched.