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 [] eg str = "text1 [text2]" or str = "text1 (text2)", otherwise works fine. How to solve this?
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.