syntax issues while searching text in docs

963 views
Skip to first unread message

Jeremy Laing

unread,
Mar 29, 2022, 4:34:38 AM3/29/22
to Google Apps Script Community
Im trying to replace text that matches [X] or [O] exactly. with the [] included. using the replaceText method and it uses googles regexp syntax

from what i can tell is there no way to specify exactly that I only want [ and ]? because everything I see is either \* for all punctuation, or [[:punct:]] for the same result. I cant use these are there are a few cases were data will be -X- which would also be replaced. 

here is the last code i used that worked:
reqs.getParagraphs().forEach(element => body.appendParagraph(element.copy().editAsText().replaceText('[[:punct:]]O[[:punct:]]',notCompletedEmoji).asParagraph().copy().editAsText().replaceText('\[X\]',completedEmoji).asParagraph()));

like i said this works but it also hits -X- or -O- which I cant have it doing.
any tips?
im generally new to regexp but i looked through the reference a million times and tried what i thought would work.

Clark Lind

unread,
Mar 29, 2022, 11:15:17 AM3/29/22
to Google Apps Script Community
I was able to get the following to work. Because of the funky regex Google uses, you have to escape the backslash.. the backslash that is escaping the brackets:
function myFunction() {
  var body = DocumentApp.getActiveDocument().getBody();
  body.replaceText("(\\[X\\])", "😀");
  body.replaceText("\\[O\\]", "🤣");
}

Jeremy Laing

unread,
Apr 3, 2022, 11:17:22 AM4/3/22
to google-apps-sc...@googlegroups.com
thank you that worked :D

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/ix8x7ICAX2o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f05114cc-f245-4d81-aaed-45467640bc27n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages