0

I'm trying to do a search and replace in a Google Doc using a Google Script. A straight text search and replace like available through the web interface.

The source doc contains [schoolname] as a placemarker and I want to make a copy of that original, then search and replace [schoolname] in the new document.

I tried replaceText("([schoolname])","sometext") after reading this thread: How to use method replaceText(searchPattern, replacement) in documents Service/ text Class

I double checked the regex through http://www.regexr.com/ to no avail

Should I be using some alternative to replaceText() or just fix the regex somehow? Thanks in advance.

Edit: code segment

var newfile = sourcefile.makeCopy();
newfile.setName(sourcefile.getName().replace("[schoolname]","replacementtext"));
var newdoc = DocumentApp.openById(newfile.getId());
var paras = newdoc.getBody().getParagraphs();
newdoc.getBody().replaceText("(\[schoolname\])","replacementtext");
Community
  • 1
  • 1

1 Answers1

0

I've found ReplaceText to be very erradic, instead of ([]) as delimiter, use %%, so tou don't have to escape.

Kriggs
  • 3,731
  • 1
  • 15
  • 23
  • Wow. That absolutely worked. I used `replaceText("%schoolname%","replacementtext")` and it worked as expected. Thanks @Kriggs – pumpkinslayer Mar 25 '15 at 14:34