go to previous page   go to home page   go to next page



Possessive Quantifiers

An quantifier in a regular expression may be greedy (the default), reluctant, or possesive. A possesive quantifier does this:

An quantifier is made possessive by appending a plus sign. The expression X++ matches X one or more times, and never gives up what it has matched. For example, the expression


does match the string


The X++ part of the expression eats up all the X's leaving the Z to be matched by the rest of the expression.

However, the expression does not match the string


because the X++ eats the entire expression, and does not give up the last X for the code [A-Z]+ to match.

Java regular expressions support possessive quantifiers, but JavaScript does not, so there interactive applet on this page.


Is the entire string


matched by the regular expression


(Note: If you try this in the applet, remember that the applet finds substrings that match the regular expression. When the entire string matches the expression, "Group 0" and "String" will be identical.)