18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Regular Expressions<br />

found, a reluctant quantifier continues to add characters from the string until either a match is found or<br />

the entire string is checked without a match. Reluctant quantifiers work in reverse of greedy quantifiers.<br />

A Possessive quantifier only tries to match against the entire string. If the entire string doesn’t produce a<br />

match, no further attempt is made. Possessive quantifiers are, in a manner of speaking, a one-shot deal.<br />

What makes a quantifier greedy, reluctant, or possessive? It’s really all in the use of the asterisk, question<br />

mark, and plus symbols. <strong>For</strong> example, the question mark alone (?) is greedy, but a question mark followed<br />

by another question mark (??) is reluctant. To make the question mark possessive, append a plus<br />

sign (?+). The following table shows all the greedy, reluctant, and possessive versions of the quantifiers<br />

you’ve already learned.<br />

Greedy Reluctant Possessive Description<br />

? ?? ?+ Zero or one occurrences<br />

* *? *+ Zero or more occurrences<br />

+ +? ++ One or more occurrences<br />

{n} {n}? {n}+ Exactly n occurrences<br />

{n,m} {n,m}? {n,m}+ At least n but no more than m occurrences<br />

{n,} {n,}? {n,}+ At least n occurrences<br />

To illustrate the differences among the three kinds of quantifiers, consider the following example:<br />

var sToMatch =”abbbaabbbaaabbb1234”;<br />

var re1 = /.*bbb/g; //greedy<br />

var re2 = /.*?bbb/g; //reluctant<br />

var re3 = /.*+bbb/g; //possessive<br />

You want to match any number of letters followed by bbb. Ultimately, you’d like to get back as matches<br />

“abbb”, “aabbb”, and “aaabbb”. However, only one of the three regular expressions returns this result,<br />

can you guess which one?<br />

If you guessed re2, congratulations! You now understand the difference between greedy, reluctant, and<br />

possessive quantifiers. The first regular expression, re1, is greedy and so it starts by looking at the<br />

whole string. Behind the scenes, this is what happens:<br />

re1.test(“abbbaabbbaaabbb1234”);<br />

re1.test(“abbbaabbbaaabbb123”);<br />

re1.test(“abbbaabbbaaabbb12”);<br />

re1.test(“abbbaabbbaaabbb1”);<br />

re1.test(“abbbaabbbaaabbb”);<br />

//false - no match<br />

//false - no match<br />

//false - no match<br />

//false - no match<br />

//true – match!<br />

So the only result that re1 returns is “abbbaabbbaaabbb”. Remember, the dot represents any character,<br />

and b is included, therefore “abbbaabbbaaa” matches the .* part of the expression and “bbb” matches<br />

the bbb part.<br />

203

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!