↑
Main Page
Browser support for possessive quantifiers
For the second regular expression,
re2
, the following takes place behind the scenes:
re2.test(“a”); //false - no match
re2.test(“ab”); //false - no match
re2.test(“abb”); //false - no match
re2.test(“abbb”); //true – match!
//store this result and start with next letter
re2.test(“a”); //false - no match
re2.test(“aa”); //false - no match
re2.test(“aab”); //false - no match
re2.test(“aabb”); //true – match!
re2.test(“aabbb”); //true – match!
//store this result and start with next letter
re2.test(“a”); //false - no match
re2.test(“aa”); //false - no match
re2.test(“aaa”); //false - no match
re2.test(“aaab”); //true – match!
re2.test(“aaabb”); //false - no match
re2.test(“aaabbb”); //true – match!
//store this result and start with next letter
re2.test(“1”); //false - no match
re2.test(“12”); //false - no match
re2.test(“123”); //false - no match
re2.test(“1234”); //false - no match
//done
Since
re2
contains a reluctant quantifier, it returns
“abbb”
,
“aabbb”
, and
“aaabbb”
, just as you’d
expect.
The final regular expression,
re3
, actually has no result because it’s possessive. Here’s what it does
behind the scenes:
re3.test(“abbbaabbbaaabbb1234”); //false – no match
Because possessive quantifiers only do one test, if that test fails, you get no result. In this case, the
“1234”
at the end of the string causes the expression not to match. If the string were simply
“abb-
baabbbaaabbb”
, then
re3
would have returned the same result as
re1
.
Browser support for possessive quantifiers leaves much to be desired. Internet
Explorer and Opera don’t support possessive quantifiers and throw an error when
you try to use one. Mozilla won’t throw an error, but it treats possessive quantifiers
as greedy.
204
Chapter 7
10_579088 ch07.qxd 3/28/05 11:38 AM Page 204
Free JavaScript Editor
Ajax Editor
©
→