Chapter 3. Regular Expression Essentials

The difficult part using regular expressions is usually the searching; once you've formulated a regex that finds exactly what you want to find, replacing things isn't too hard. In this section on regex essentials we'll therefore deal just with matching (i.e. searching). Also, most of the examples use simple text strings for which, in fact, you don't need InDesign. It is usually enough to define your own text string and try a regex against that, as illustrated earlier and repeated here for convenience:

myString = 'A pound of venison in weight is eight pounds in money.';
myPounds = myString.match( /pounds?/g );

These two lines are a script in their own right and you don't have to have InDesign running. The regexes used as examples in this section work on the same basis.

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.