I am trying to write a google app script, which will find and replace specific words with others in google docs...
I would like " hello " (space, hello, space) to be replaced by " R1 " (space, R1, space) And if there is any punctuation mark after hello like a period, comma or question mark it should be the same logic:
" hello "
to be replaced by" R1 "
" hello. "
to be replaced by" R1. "
" hello, "
to be replaced by" R1, "
" hello? "
to be replaced by" R1? "
So I used the following:
function docReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText(" hello ", " R1 ");
body.replaceText(" hello. ", " R1. ");
body.replaceText(" hello, ", " R1, ");
body.replaceText(" hello? ", " R1? ");
}
Unfortunately this doesn't work, as "." , "," and "?" are regex symbols.
Then, I tried this:
function docReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.replaceText(" hello ", " R1 ");
body.replaceText(" hello\. ", " R1. ");
body.replaceText(" hello\, ", " R1, ");
body.replaceText(" hello\? ", " R1? ");
}
But still doesn't work. Commas and Question marks return as periods.
I would appreciate if anyone could help with the correct code.