Find spaces only (no returns or tabs) with InDesign’s GREP

When learning GREP, the syntax for a space is usually shown as \s. This works in most situations, but it is worth pointing out that this syntax represents any space, including tabs and returns, not just horizontal space between words. For finding spaces only but ignoring returns and tabs, we need a different GREP string.

Take the following example where there are double spaces (or worse) in a text frame. With the assistance of the GREP editor script from Peter Kahrel, we can identify every result that will be found using the \s syntax plus the {2,} (identifies two or more of the same result).

The yellow highlights show the search results and while the double spaces have been identified, so have the double returns and space plus returns. If all of these spaces were to change to single spaces, all returns would be lost and the type would all be in one paragraph – an unintended result.

There is a pre-made GREP search built into the find/change dialog and it is called Multiple Space to Single Space, and the code looks like this:

[~m~>~f~|~S~s~<~/~.~3~4$ ]{2,}

If this is keyed into the GREP editor, the result is as follows:

The double spaces and longer can now be found without accidentally selecting returns or tabs, but the GREP code is quite unwieldy. Perhaps it can be copied each time it is required – let’s go to the find/change dialog box, call up the Multiple Space to Single Space search and copy the result.

Now let’s paste it into the GREP editor

The syntax hasn’t placed, but instead been represented by the actual characters that the GREP code represented. I can type the required syntax manually, but it’s a long piece of code that I don’t want to remember all of the time.

Use \p{Zs} instead

InDesign’s GREP allows for specific Unicode properties that aren’t available from the find/change dialog box, but they are accessible via the GREP editor script. Click on the insert wildcard or character class button in the top left of the GREP editor dialog box.

A new dialog box will open. From here, unfurl the Unicode properties, then unfurl the separator, and choose Space Separator.

This dialog also includes other Unicode properties that may be of interest, but for now it reveals that the syntax to use is \p{Zs}. Let’s try that in the GREP editor, along with the {2,} to find two or more instances of the same syntax.

Fantastic, it has done the same thing that InDesign’s Multiple Space to Single Space has done with a quarter of the syntax.

Additionally, now that the syntax is known, we don’t have to go through the GREP editor subdialog each time, the syntax is easy to remember – \p{Zs}.

Lastly

The GREP editor script is a fantastic utility made by Peter Kahrel that allows users to see the results of GREP code in real-time, and also provides access to snippets of GREP code that aren’t available from InDesign’s own find/change panel. An added bonus is that searches can also be directly applied to paragraph styles as GREP styles, rather than copying the syntax and pasting it into the paragraph style (or rewriting it on paper and re-keying it because the GREP gets translated into what it actually represents). I highly recommend this script and if you find it useful as well, make sure you let Peter know by making a donation his way.

Additionally, Peter is the author of GREP in InDesign: an InDesignSecrets guide – a fantastic resource for anyone wishing to learn how to use GREP in Adobe InDesign; or to build more sophisticated searches. If you would like to purchase a copy of this title, please click on this affiliate link here.

About colmin8r
A prepress operator and graphic designer for a South Australian commercial printer, with close to 20 years of experience in the trade. He is also a regular contributor to this site and InDesign Magazine, and hosts his own prepress blog "Colecandoo".

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: