Reputation: 179
I am trying to find elements in Google Doc by matching strings. I am using the following code:
var str = "text1 (text2)"
var doc = DocumentApp.openById(docid);
var body = doc.getBody();
var element = body.findText(str);
Logger.log(element)
The code gives null if the string to be matched contains () or [] e.g. str = "text1 [text2]" or str = "text1 (text2)", otherwise works fine. How to solve this?
Upvotes: 1
Views: 681
Reputation: 1987
According to the documentation, findText() receives a pattern, so try escaping the parenthesis and brackets with \\
. For example:
const text = "text1 \\(text2\\)";
const body = DocumentApp.getActiveDocument().getBody();
const element = body.findText(text);
Upvotes: 2