How to search for the Nth (2nd, 3rd, etc.) white space in a paragraph?

How to search (via GREP) for the Nth (2nd, 3rd, etc.) white space in a paragraph in Adobe Indesign?

Answer

Solution

Find what

(([^\s]*\s?){n})\s(.*\r) where n is a number of your choice.

Change to

$1text$3 where text is the text you want to change the nth space to.


Explanation

Find what

(           // begin parenthesis 1
  (         // begin parenthesis 2
    [^\s]*  // find zero or more non white spaces
    \s?     // followed by zero or one white space
  )         // end parenthesis 2 (assign the parenthesis to $2 - not used)
  {n}       // repeat everything in parenthesis 2 "n" times ("n" must be a number)
)           // end parenthesis 1 (assign the parenthesis to $1)
\s          // followed by a white space (the one we are looking for)
(           // begin parenthesis 3
  .*        // followed by zero or more characters
  \r        // followed by end of paragraph
)           // end parenthesis 3 (assign the parenthesis to $3)

Change to

$1          // keep everything from parenthesis 1 (before the wanted white space)
text        // change the found white space to "text" of your choice
$3          // keep everything from parenthesis 3 (after the wanted white space) 

Attribution
Source : Link , Question Author : i. Madar , Answer Author : Wolff

Leave a Comment