Using Google Apps Scripts replaceText() isn't working with the question mark

Viewed 689

I have a Google Apps Script script that I wrote that uses:

paragraph.replaceText(sentences[j], edit);

If sentences[j] ends with a question mark, the resulting change is equivalent to

edit + "?"

For example:

paragraph.replaceText("How are you?", "Hello world!");

changes my document to "Hello World!?" instead of "Hello World!"

This only happens with question marks. I have tested it with periods, exclamation marks and ellipsis.

I have tried a few things:

  • replaceText("?","stuff") and gotten invalid regular expression for the "?".

  • replaceText(/\?/, "stuff") and the question marks don't change.

  • replaceText(/\?\?/, "?") doesn't throw an error, it just doesn't change anything.

To create sentences I do

var lines = paragraph.getText().split(/\b(\?|\!|\.\.\.|\u2026|\. \. \.|\.|\n\r|\r\n|\n|\r)/g);
var sentences = [];
for(j=0;j < lines.length;j = j+2){
  sentences.push(lines[j]+lines[j+1]); 
}

and it catches question marks just fine. Anyone have any clue how to fix this?

1 Answers
Related