6.3. Double-Barreled Regex: Add a Thin Space

Many reference conventions specify that page references take the form "Author Year: Pages," e.g. Bloomfield 1956: 22. Possibly there is a letter after the year, resulting in formats such as Leech 2005b: 82. Some publishers want a thin space after the colon, while texts are submitted typically with either a space or nothing between the colon and the following digit. So we need to find sequences of a year perhaps followed by a letter, followed by a colon, maybe followed by a space, followed by a digit. In all these, a thin space needs to be inserted after the colon, deleting the space there if there is one. The following script handles this:

var myDoc = app.activeDocument;
// find year letter? colon space? digit
var colons =documentContents( myDoc ).match(/\d\d\d\d[a-z]?:\s?\d/g )
if( colons != null )
     {
     app.findPreferences = app.changePreferences = null;
     for( var i = 0; i < colons.length; i++ )
          // replace colon+space (if any space present) with colon+thin space
          myDoc.search( colons[i], false, false, colons[i].replace( /:\s?/,
':\u2009' ));
     }

In each iteration, colon[i] is the search argument, the replace argument is derived from it by a regular expression. The regex is needed here because some colons in the array are followed by a colon, other are not: that's what we matched with the regex to collect the colons.

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.