0

Im trying to run a very basic Google Script on a document. docBody = docBody.replaceText(searchStr, replaceStr);

It works perfectly when the searchStr = 'Name' for example, but when I try to replace '[Name]' the script hangs and produces garbage in the document. I realise that the searchStr parameter is a regular expression and I have tried :

\[Name\]

/\[Name\]/g

(\[Name\])

All with no success...

Any suggestions?

1
  • I've read elsewhere that you may need to double-escape the square brackets even in single-quoted strings. I'm not sure I believe this, but it's worth a shot. Commented Oct 30, 2012 at 19:47

1 Answer 1

2

If you're doing

searchStr = '\[Name\]';

then that should work, without slashes, without modifiers, without groups. If you're using double-quotes, make sure to double-escape:

searchStr = "\\[Name\\]";
Sign up to request clarification or add additional context in comments.

1 Comment

Double quotes with double slashes did it, single quotes with single slashes not.. Thanks a mil!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.