4.3. Showing Section Numbers

In a text which uses manually numbered sections, all sorts of things can go wrong with those numbers, so it would be good if we could check these. The following regular expression does this:

/\r[\d\.]+/g

The \r stands for the paragraph mark, making sure we match only sections at the beginning of lines. The following one-script prints a list of all section numbers in the console; select a text frame or an insertion point in an InDesign document and run it:

app.selection[0].parentStory.contents.match( /\r[\d\.]+/g )

There could well be some things in the output that aren't section headings as the script matches any line that starts with a sequence of digits and dots.

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.