Computer Science Canada

[Regex-tut] Nested Capturing Groups

Author:  wtd [ Sat Nov 13, 2004 11:04 pm ]
Post subject:  [Regex-tut] Nested Capturing Groups

Thus far...

As it is, I've only shown examples of capturing groups which are laid out linearly. The previous example was:

code:
/(['"])(.*)\1/


What if...

What if I want to match that entire thing, including the quotes?

code:
/((['"]).*\2)/


Notice that the parentheses are now nested, and \1 has become \2. This is because group 1 is now the entire thing. Group 2 is the quote.

The simple rule

The opening parenthesis determines the number of the group.

Another group following:

code:
((['"]).*\2)


Would be group 3.


: