Reputation: 952
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.
Upvotes: 0
Views: 184
Reputation: 11268
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 + ">>",'');
}
Upvotes: 2