Flash CS3 Documentation |
|||
| Programming ActionScript 3.0 > Using regular expressions > Regular expression syntax > Quantifiers | |||
You use quantifiers to specify repetitions of characters or sequences in patterns, as follows:
|
Quantifier metacharacter |
Description |
|---|---|
|
|
Matches the previous item repeated zero or more times. |
|
|
Matches the previous item repeated one or more times. |
|
|
Matches the previous item repeated zero times or one time. |
|
and
|
Specifies a numeric quantifier or quantifier range for the previous item:
|
You can apply a quantifier to a single character, to a character class, or to a group:
/a+/ matches the character a repeated one or more times./\d+/ matches one or more digits./[abc]+/ matches a repetition of one or more character, each of which is either a, b, or c./(very, )*/ matches the word very 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:
{0} and {0,0} do not exclude an item from a match./abc+*/.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.
Flash CS3