Lookaround and Formatting
With lookbehind and lookahead you can match text when it is preceded or followed by certain text. This is limited to text only: you can say, “Find an f followed by a closing parenthesis” (using the expression f(?=\))), but you can’t create an expression that says, “Find an italic f followed by a roman closing parenthesis.” In other words, any formatting that you set in Find Format applies to the whole GREP expression.
However, with some GREP trickery it is possible to match text depending on the presence of formatting. It is easy to convert formatting codes to text tags (HTML-like tags, for instance). With such text tags you can then use lookaround to find roman text only when it is preceded or followed by a tag that represents italic text (or any other formatting, but we’ll use italic here for illustration). When you’re done, you convert from tags back to formatted text.
Changing Formatting to Text Tags
This is much less complicated than it sounds, and a few examples will bear that out. For example, to mark all italic with the tags <i> and </i> use these expressions:
Find what: |
.+ |
Find format: |
+Italic |
Change to: |
<i>$0</i> |
Change format: |
+Regular |
This adds <i> wherever a stretch of italic text begins and </i> wherever it ends. It also removes all italic from the text, which, as we’ll see later, is necessary. Thus, a sentence such as “let’s get rid of all italics codes” will appear as “let’s get rid of <i>all</i> italic codes.” Now that the ...
Get GREP in InDesign 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.