Skip to main content
Participating Frequently
June 8, 2011
Answered

Help with GREP entry formatting in a Harbs script

  • June 8, 2011
  • 1 reply
  • 712 views

I’m using InDesign CS5 and I’m sorry, I am ignorant of scripting and GREP. I’m hoping there is a quick, simple solution one of you can offer for my problem.

I am using a free script written by Harbs called ReplaceHyperlinkUrlValues.jsx to replace part of each URL hyperlink in an InDesign document to account for a move to a new server. The script presents just Find and Replace entry boxes. If I want to find …Server1… in each URL and replace it with …Server2…, I enter those terms and it works wonderfully (I have 700 URLs to change).

However, I need to replace …Server1\vol1… with just …Server2... This doesn’t work, presumably because there is a backslash character involved. Can you tell me how to enter the Find term?

(On searching the web, one tip I found was to enclose the find term in single quotes, but it didn’t work.)

This topic has been closed for replies.
Correct answer Jongware

You have not one but possibly two problems here

The backslash is a special character in GREP; it's used to 'escape' characters that otherwise are GREP instructions. So if you want to search for a single period ., GREP will appear to malfunction because the period itself is a GREP code (it stands for "any character at all"). Preceding it with a backslash will remove the magic properties and make it a "literal" character instead: \. inside a GREP string will search for a single period, without the backslash.

So a backslash is a magic code and is used to make the next character loose its magic ... then, logically, you need two backslashes to find only one! (That deserves an exclamation point, because it's entirely true.)

You can try this, and it's possible it works, but if it doesn't:

The backslash is also a special character in Javascript. ... And it does the same as in GREP as well. You might think, "good, then there is no problem", but then you are wrong.

1. Javascript reads your string and finds a double backslash. It knows backslashes are special, so it will "parse" this code and actually store a single backslash into memory.

2. The string from memory is handed over to InDesign's GREP function, which sees a single backslash, and tries to make it a special code with the character next to it (I don't think "\v" is a valid code, but in any case it'll not do what you think it'll do).

3. The search fails and leaves you scratching your head.

So if the above fails -- a double backslash where you previously used a single one --, you can try with a double for each of the backslashes: "\\\\".

1 reply

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
June 8, 2011

You have not one but possibly two problems here

The backslash is a special character in GREP; it's used to 'escape' characters that otherwise are GREP instructions. So if you want to search for a single period ., GREP will appear to malfunction because the period itself is a GREP code (it stands for "any character at all"). Preceding it with a backslash will remove the magic properties and make it a "literal" character instead: \. inside a GREP string will search for a single period, without the backslash.

So a backslash is a magic code and is used to make the next character loose its magic ... then, logically, you need two backslashes to find only one! (That deserves an exclamation point, because it's entirely true.)

You can try this, and it's possible it works, but if it doesn't:

The backslash is also a special character in Javascript. ... And it does the same as in GREP as well. You might think, "good, then there is no problem", but then you are wrong.

1. Javascript reads your string and finds a double backslash. It knows backslashes are special, so it will "parse" this code and actually store a single backslash into memory.

2. The string from memory is handed over to InDesign's GREP function, which sees a single backslash, and tries to make it a special code with the character next to it (I don't think "\v" is a valid code, but in any case it'll not do what you think it'll do).

3. The search fails and leaves you scratching your head.

So if the above fails -- a double backslash where you previously used a single one --, you can try with a double for each of the backslashes: "\\\\".

mpc999Author
Participating Frequently
June 8, 2011

Thank you, Jongware. The double backslash worked.

I really appreciate your help. It, together with Harbs' script, saved me a ton of work.