I’m trying to use regular expressions to search through a Google Document that contains code blocks marked with triple backticks. However, when I run my script, the findText() method keeps returning null instead of finding the matches.
Here’s what I’m working with:
var tripleBacktickPattern = '`{3}([\s\S]*?)`{3}'; // Pattern to match content between triple backticks
var searchResult = documentBody.findText(tripleBacktickPattern); // This returns null
I think the issue might be that my regex pattern uses something that isn’t supported by the RE2 regex engine that Google Apps Script uses. Has anyone run into this problem before? What regex syntax works reliably with the findText() method in Google Docs?
The *?
non-greedy quantifier is probably causing your issue - RE2 can be finicky with it. I’ve hit the same wall with Google Docs API. Try a greedy approach: {3}([\s\S]*){3}
but heads up - this might grab too much if you’ve got multiple code blocks. What worked better for me: search for the opening triple backticks first, then use getRangeBuilder()
to manually parse until you hit the closing backticks. More code, but you get way better control. Google Apps Script’s findText method is pretty limited compared to standard JavaScript regex. Sometimes mixing basic patterns with manual text processing just works better.
yeah, i’ve hit this before. the problem’s likely [\s\S]
- RE2 gets weird with that in google docs. use [^]
instead to match any character including newlines. so try {3}([^]*?){3}
. fixed the same multiline matching issue for me in apps script.
I’ve encountered similar challenges when working with the findText method in Google Apps Script. The limited regex support in RE2 can be a real headache. Instead of using a complex regex, I recommend breaking your task into smaller parts. Start by isolating the triple backticks with a simple findText method, and then implement a loop to manually track the content until the closing backticks are found. Additionally, you might consider using the Document service’s searchText method, as it could provide better results despite its own quirks.