5.1. Replace Using Wildcards

The strength of regex replacements comes out in the use of backreferences. Earlier we saw that backreferences in the regex itself are written as \1 for the first backreference, \2 for the second one, etc. (up to and including 99). When used in the replace argument, they are written '$1', '$2', etc.; like all other replacement text, they must be in quotes.

In the previous section we've been matching some date formats, so let's see what we can do to change them. The following example swaps the first two numbers, the day and the month (or vice versa), in date representations of the form dd-mm-yyyy:

myDates = '3-8-2006 12-5-1999 3-11-1976 10-12-2005';
myDates.replace( /(\d\d?)-(\d\d?)-(\d\d\d\d)/g, '$2-$1-$3' );

The regex creates three capture groups: the first one contains the days, the second one, the month, and the third one stores the year. In the replacement argument you use $n to refer to the capture groups, using any order you want; the script here simply swaps the first two numbers. To cater for different types of separator, create capture groups for them as well (we break lines here after the parenthesis and the comma for clarity only):

myDates.replace(
     /(\d\d?)([-\/\.])(\d\d?)\2(\d\d(\d\d)?)/g,
     '$3$2$1$2$4' );

The first capture group, (\d\d?), is a one- or two-digit number; the second capture group, ([-\/\.]), is the separator, which can be a hyphen, a forward slash, or a dot (as defined here); the third group is another one-or two-digit ...

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.