How to Find and Fix Partially Italic Words with GREP
If you’ve ever been stuck finding and fixing a bunch of partially italic words in InDesign, get ready to be amazed by this GREP solution posted by Dhafir Photo in our forums.
The problem:
The solution:
Open Find/Change and in the GREP tab, enter the following:
In Find What: [\S\h]+
In Find Format: + Italic
Together, these conditions match everything in italic except breaks.
In Change To: $0~j
(Add a non-joiner after the found text.)
The purpose of the non-joiner is to be a zero-width marker at the end of italic formatting.
After running the Find/Change, you can see the non-joiners if you show hidden characters. They look like carons (upside down carets).
Now, we need to find all the non-italics before and after the non-joiner and make them italic. For that, enter the following:
In Find What: \p{l*}*~j\p{l*}*
(any letter of any language, zero or more times, followed by a non-joiner, followed by any letter of any language, zero or more times)
In Change Format: + Italic
Now we have the formatting we want.
The only thing left to do is clean up the non-joiners, with the following:
In Find What: ~j
Leave Change To empty.
The final result:
This is definitely going to be useful.
In fact, I just did a little experimenting and found preliminary solutions to a problem I’ve wanted to solve since I started using InDesign (either ID 2.0 or CS, 2003-ish, when I’d never heard of GREP and don’t even know if ID had it): to search for non-italic characters (usually punctuation) touching italic ones. For example, a non-italic comma/semicolon/colon/etc. at the end of an italicized word, or non-italic parentheses at either end (or both ends) of text in italics. I asked about it in a GREP session at PepCon 2014 (was told it couldn’t be done). Have posted about it in the forums more than once. In 2012 (https://creativepro.com/topic/possible-to-find-unstyled-characters-touching-styled-characters-on-eitherboth-sides/) Jongware provided GREP that could pretty much do the job with some workarounds, but none of those expressions work anymore.
I’m sure these can be made more powerful or more efficient, but I’m ecstatic at the results I got from 5 minutes of effort.
For the parentheses issue I used [Sh]+ (as above) but altered the Change To field to put non-joiners both before and after the italics: ~j$0~j.
Then I did a find query that looks for ( followed by a non-joiner OR a non-joiner followed by ) and changes it to italics: ((()(~j)|(~j)()))
I’d love to be able to have it look for (~j and check to see whether the next ) is preceded by ~j and only change them if both are matches. Possible? (Would also be useful for quotation marks around italic text, but would have to account for punctuation inside the one at the end.)
To address the commas/semicolons/colons issue that has haunted me for 20 years, I first ran this article’s search to put a non-joiner after italic text, then searched for non-joiner followed by one of those: (~j)(,|;|:) with my italic character style in the Change Format box.
These are going to save me a ton of time–even though I’ll probably never be confident enough to click Change All. On some books this has taken me a full day—or more—and I always knew I probably missed some and/or changed some I shouldn’t have.
Thank you!
(Apologies for the length of this comment. Hopefully it will help someone else and maybe lead to further refinements and ideas.)
Nice!
Very clever, Mike! Also, now I know what a “caron” is? The more you know…
Oh, I see now that this solution originated on the Forums from Dhafir Photo. Thank YOU! And thank you Mike for writing it up!
Thanks Mike! For the sake of clarity, S and h in “[Sh]” stands for…