Chapter 7. Variables in Regexes

Recall the example of the regex which matched page ranges. We constructed that as a string so that it could be broken across lines to make it readable and insert comments, even though constructing regexes as a string can make them slightly harder to read. Another reason for constructing regexes as strings is when you want to pass variables to them. This can be illustrated with a script that applies character styles to certain Unicode ranges. First we'll show a script that deals with just one range to get our bearings, then we'll generalize it to handle any range. This script applies the character style phonetics (which we assume is present, for the moment) to any character from the phonetic range:

// collect all characters from Unicode range Phonetic
var myMatch = documentContents.match( /[\u0250-\u02FF]/g);
if( myMatch != null )
     {
     myMatch = deleteDuplicates( myMatch );
     app.findPreferences = app.changePreferences = null;
     // set character style for replacement
     app.changePreferences.appliedCharacterStyle =
app.activeDocument.characterStyles.item('Phonetics');
     for( var i = 0; i < myMatch.length; i++ )
          app.activeDocument.search(    myMatch[i], false, false, myMatch[i] );
     }

But suppose you want to apply character styles to Cyrillic, Hebrew, and symbols, too. Rather than repeating the script for each Unicode range, we want to feed the regex with different values. The regex in which the range is fed as a variable could look as follows:

 var myregex = '[' ...

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.