Quantifiers
You use quantifiers to specify repetitions of characters or sequences in patterns, as follows:
Quantifier metacharacter | Description |
---|---|
*(star) | Matches the previous item repeated zero or more times. |
+(plus) | Matches the previous item repeated one or more times. |
?(question mark) | Matches the previous item repeated zero times or one time. |
{n} {n,} and {n,n} | Specifies a numeric quantifier or quantifier range for the previous item: /A27/ matches the character A repeated 27 times. /A{3,}/ matches the character A repeated 3 or more times. /A5/ matches the character A repeated 3 to 5 times. |
You can apply a quantifier to a single character, to a character class, or to a group:
/a+/
matches the charactera
repeated one or more times./\d+/
matches one or more digits./[abc]+/
matches a repetition of one or more character, each of which is eithera
,b
, orc
./(very, )*/
matches the wordvery
followed by a comma and a space repeated zero or more times.
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 Flags and properties.