is now part of CreativePro.com!

Formatting Important Phrases in InDesign with GREP

7

In the past, I have worked with clients to build catalogs where the phrase “USA made” requires special formatting. Typically, the client wants “USA” to be red, and “made” to be blue. But only when “USA” precedes “made,” and “made” follows “USA.” This seems complicated but is a pretty understandable GREP exercise when it is broken down into pieces.  So I thought it would be worth a blog post.

Granted, you probably don’t need to format “USA made” in red and blue, but the need to format two words differently only when they appear together is a common design pattern, so hopefully you can learn from this example and adapt it for your own use.

We’re going to do this with a GREP paragraph style. You can learn more about GREP paragraph styles here and here.

1. Create some sample text that includes the phrase “USA made” in a few separate locations.

2. Create a new Paragraph style called “body text” and apply this to the text.

3. Create a new Character style called “red” that is formatted with the color red.

4. Create a new Character style called “blue” that is formatted with the color blue.

5. Edit the “body text” paragraph style, and fill in the GREP Style section exactly as shown below.

Let’s break the 2 GREP expressions down into simple pieces:

Expression 1:

\<USA\>\s+(?=\<made\>)

USA will match the word “USA”. GREP expressions are case-sensitive by default, so it will only match “USA” in all caps.

Adding \< and \> around “USA” tells InDesign to match only a whole word. Probably not really necessary in this case, since I can’t think of any English words that begin or end in all-caps “USA”. But if we were dealing with different words, this would be important.

\s is shorthand for a “whitespace” character. This matches any type of space. Regular spaces, non-breaking spaces, em spaces, en spaces, etc.

The + means “one or more” so we are looking for “USA” followed by one or more spaces followed by “made”.

However, we don’t want to apply the “red” character style to the entire phrase “USA made”, only to “USA”. So we need to wrap the \<made\> string in parentheses with ?= at the front. (?=) is called a “positive lookahead”. In this case, it tells InDesign that when it finds an occurrence of “USA”, it should “look ahead” and see if it is followed by “made”. If it is followed by “made”, then consider it a match, but don’t include “made” in the match.

Expression 2:

(?<=\<USA\>)\s+\<made\>

This expression is much like expression 1, but the opposite.

\s is shorthand for a “whitespace” character. This matches any type of space. Regular spaces, non-breaking spaces, em spaces, en spaces, etc.

The + means “one or more”, so we are looking for one or more spaces.

made will match the word “made”, but only when all lowercase.

Adding \< and \> around “made” tells InDesign to match only a whole word.

So we are looking one or more spaces followed by the whole word “made”.

However, we don’t want to apply the “blue” character style to the entire phrase “USA made”, only to “made”. So we need to wrap the \<USA\> string in parentheses with ?<= at the front. (?<=) is called a “positive lookbehind.” In this case, it tells InDesign that when it finds an occurrence of “made”, it should “look behind” and see if it is preceded by “USA”. If it is preceded by “USA”, then consider it a match, but don’t include “USA” in the match.

Hopefully you’ve learned a thing or two from this exercise, and can use one or more of these techniques in one of your own projects!

Keith Gilbert is a design consultant, developer, educator, speaker, and author. His work has taken him throughout North America, Africa, Europe, and Asia. During his 35+ year career his clients have included Adobe, Apple, Target, Oracle, and the United Nations. He is the author of several popular titles for LinkedIn Learning, Adobe Press, and CreativePro. Find him at gilbertconsulting.com and on Twitter @gilbertconsult
  • Is there anything GREP can’t do? Well, maybe. While it can work wonders like the above, it still seems unable to distinguish web addresses from other text. I’ve got a number of GREP scripts that be far more useful if GREP S&R inside ID allowed me to specify whether or not web addresses are to be included in the search.

    A typical case involves replacing hyphens with n-dashes or m-dashes, In the scientific books I work with, perhaps half the hyphens will be part of web addresses where that hyphen is never to be changed. I haven’t found a way to tell ID to skip that text. Is there anyway to do a search that’d exclude all Endnote Text paragraph styled text? That’d eliminate most of them.

    And yes, I realized that detecting an entire web address, particularly its end, is not easy. A few years back I tried to find a GREP script that does that flawlessly and drew a blank. And I realize that ultimately the flaw lies with GREP itself and not its ID implementation.

    Still, it would be nice if GREP intrinsically knew if text was part of a web address or not. In that, GREP seems to still live the world of UNIX pre-web. It illustrates a product so good, few realize that it’d benefit from a few enhancements.

  • Anne-Marie Concepcion says:

    GREAT lesson in GREP features via this one “USA made” example. Thank you!

    Which Dr. Seuss book has a Thneed?

  • Ellis says:

    Is it possible to format an entire paragraph if certain text is present? I have some paragraphs that include the letters “QS” and wondered if the paragraph could change color wherever QS occurs.

  • Vatrice Chestnut says:

    Thanks for the lesson. I am trying to have my company name appear in blue text in every instance in my proposals. I followed the steps for Expression 1 but it’s not working for me. I believe I have an error in the code somewhere. Here’s what I created: (?<=) Are you able to tell me what’s wrong?

  • >