0

I am looking for regular expression syntax that will replace all types of special characters.

I have a Google document with question titles between << and >> markers. I am trying to replace all markers with the form response (for simplicity I am replacing it with a blank value ' ' in the script example below).

My current script:

var form = FormApp.getActiveForm();
var body = DocumentApp.openById('').getBody();
var items = form.getItems();
  for(i=0;i<items.length;i++){
    body.replaceText('<<items[i].getTitle()>>','');
 }

This falls over when a user includes a question mark ?, brackets () or an apostrophe ' (and I am sure many others) in their question title.

1 Answer 1

2

You can consider escaping the special characters such as '*?()|' that are used in regular expressions. Here's a sample:

  var form = FormApp.getActiveForm();
  var body = DocumentApp.openById('').getBody();
  var items = form.getItems();
  for(i=0;i<items.length;i++){
    var title = items[i].getTitle();
    title = title.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');    
    body.replaceText("<<" + title + ">>",'');
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much Amit

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.