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" ...

Get JavaScript Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.