I’m trying to use the findText()
function of the DocumentApp
class to get the position of some text inside a Google Docs. To do so, I’m using a Regex (RE2) expression. I want it to find the following sequences: $$anyCharacterHere$$
; basically anything enclosed in between $$ … $$. As there might be more than one of these sequences, we have to tell the regex to match any sequence of this type without any "$" inside.
I’ve tried using the following:
const re2 = new RegExp("\$\$.*(?!\$)\$\$");
let range = docBody.findText(re2)
but it says that this is an invalid expression. I tried changing the backslashes and that kind of things, but couldn’t get any further.
The goal I want to achieve is, for example, in this text:
Thi is a $$text$$ to show an $example$$ of how the $$regex$$ $$$hould$$ work.
the regex should find: $$text$$ and $$regex$$.
2
Answers
An alternative is given below where, your sample string is modified so that the searched strings can be at the beginning or in the middle or at the end of the sentence and also space characters can be involved in them.
A sample string may be like this:
$$Test at the beginning$$ and this is $$a text$$ to show an $example$$ of how the $$regex$$ $$$hould$$ work.
Screenshot of the console is given below:
Capturing all segments of strings enclosed with double dollar signs
$$
, while ignoring any single dollar signs$
.Reference: