O'Reilly logo

JavaScript Regular Expressions by Gabriel Manricks, Loiane Groner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Grouping, alternation, and back reference

In the following table, you can find the patterns for grouping, alternation, and back reference. The grouping is used to group a set of characters in a Regex. The alternation is used to combine characters into a single regular expression, and the back reference is used to match the same text as previously matched by a capturing group:

Pattern

Description

Example

(x)

This groups characters together to create a clause, that is, it matches x and remembers the match. These are called capturing parentheses.

/(foo)/ matches and remembers "foo" in "foo bar".

()

Parenthesis also serves to capture the desired subpattern within a pattern.

/(\d\d)\/(\d\d)\/(\d\d\d\d)/ matches "12", "12", and "2000" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required