3.7. Backreferences

In a regular expression you can refer back to characters matched earlier in the same regex. To enable this, you need to enclose what you want to refer back to in parentheses. A backreference is created as follows:

myString = 'focussing on travelling gaffers';
myDoubles = myString.match( /([a-z])\1/g )

The regex /([a-z])\1/ matches any double lower-case letter. First, a group is created by the range [a-z] in parentheses; this is capture group 1. It is followed by a backreference, here, \1. This regex literally says "a letter followed by the same letter." Any subsequent backreferences are numbered 2 through 99. For example, to find all repeating two-letter syllables, you could use /([a-z])([a-z])\1\2/. This matches strings such as titi in repi titi on and eses in parenth eses.

Like other characters, backreferences can be modified by repetition operators. To match any sequence of identical lower-case letters, use /([a-z])\1+/; this matches all the "r"s in "Grrrrrr!".

Get Automating InDesign with 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.