Main Page

Regular Expressions

found, a reluctant quantifier continues to add characters from the string until either a match is found or
the entire string is checked without a match. Reluctant quantifiers work in reverse of greedy quantifiers.
A
Possessive quantifier
only tries to match against the entire string. If the entire string doesn’t produce a
match, no further attempt is made. Possessive quantifiers are, in a manner of speaking, a one-shot deal.
What makes a quantifier greedy, reluctant, or possessive? It’s really all in the use of the asterisk, question
mark, and plus symbols. For example, the question mark alone (
?
) is greedy, but a question mark fol-
lowed by another question mark (
??
) is reluctant. To make the question mark possessive, append a plus
sign (
?+
). The following table shows all the greedy, reluctant, and possessive versions of the quantifiers
you’ve already learned.
Greedy Reluctant
Possessive
Description
?
??
?+
Zero or one occurrences
*
*?
*+
Zero or more occurrences
+
+?
++
One or more occurrences
{
n
}
{
n
}?
{
n
}+
Exactly
n
occurrences
{
n
,
m
}
{
n
,
m
}?
{
n
,
m
}+
At least
n
but no more than
m
occurrences
{
n
,}
{
n
,}?
{
n
,}+
At least
n
occurrences
To illustrate the differences among the three kinds of quantifiers, consider the following example:
var sToMatch =”abbbaabbbaaabbb1234”;
var re1 = /.*bbb/g; //greedy
var re2 = /.*?bbb/g; //reluctant
var re3 = /.*+bbb/g; //possessive
You want to match any number of letters followed by
bbb
. Ultimately, you’d like to get back as matches
“abbb”
,
“aabbb”
, and
“aaabbb”
. However, only one of the three regular expressions returns this result,
can you guess which one?
If you guessed
re2
, congratulations! You now understand the difference between greedy, reluctant, and
possessive quantifiers. The first regular expression,
re1
, is greedy and so it starts by looking at the
whole string. Behind the scenes, this is what happens:
re1.test(“abbbaabbbaaabbb1234”); //false - no match
re1.test(“abbbaabbbaaabbb123”); //false - no match
re1.test(“abbbaabbbaaabbb12”); //false - no match
re1.test(“abbbaabbbaaabbb1”); //false - no match
re1.test(“abbbaabbbaaabbb”); //true – match!
So the only result that
re1
returns is
“abbbaabbbaaabbb”
. Remember, the dot represents any character,
and
b
is included, therefore
“abbbaabbbaaa”
matches the
.*
part of the expression and
“bbb”
matches
the
bbb
part.
203
Regular Expressions
10_579088 ch07.qxd 3/28/05 11:38 AM Page 203


JavaScript EditorFree JavaScript Editor     Ajax Editor


©