User Guide
294 Using Regular Expressions
You can use quantifiers within parenthetical groupings that have quantifiers applied to them.
For example, the following quantifier matches strings such as
word and word-word-word:
/\w+(-\w+)*/
By default, regular expressions perform what is known as greedy matching. Any subpattern in
the regular expression (such as
.*) tries to match as many characters in the string as possible
before moving forward to the next part of the regular expression. For example, consider the
following regular expression and string:
var pattern:RegExp = /<p>.*<\/p>/;
str:String = "<p>Paragraph 1</p> <p>Paragraph 2</p>";
The regular expression matches the entire string:
<p>Paragraph 1</p> <p>Paragraph 2</p>
Suppose, however, that you want to match only one <p>...</p> grouping. You can do this
with the following:
<p>Paragraph 1</p>
Add a question mark (?) after any quantifier to change it to what is known as a lazy quantifier.
For example, the following regular expression, which uses the lazy
*? quantifier, matches <p>
followed by the minimum number of characters possible (lazy), followed by
</p>:
/<p>.*?<\/p>/
Keep in mind the following points about quantifiers:
■ The quantifiers {0} and {0,0} do not exclude an item from a match.
■ Do not combine multiple quantifiers, as in /abc+*/.
■ The dot (.) does not span lines unless the s (dotall) flag is set, even if it is followed by a *
quantifier. For example, consider the following code:
var str:String = "<p>Test\n";
str += "Multiline</p>";
var re:RegExp = /<p>.*<\/p>/;
trace(str.match(re)); // null;
re = /<p>.*<\/p>/s;
trace(str.match(re));
// output: <p>Test
// Multiline</p>
For more information, see “The s (dotall) flag” on page 301.