Home > Software engineering >  Google re2 regex, extract all inside brackets
Google re2 regex, extract all inside brackets

Time:05-04

How can I get the text inside brackets including brackets with google re2 regex? I've this text and I need to find and replace it with google app script.

[?replaceMe Lorem ipsum dolor sit amet, consectetur adipiscing elit. ?]

This regex work online but not in apps script.

\[replaceMe\?[^\]]*\?\]

CodePudding user response:

Text inside of bracket and bracket

/{[^{] }/g

function lfunko() {
  const s = "This is some {text inside of brackets}";
  Logger.log(s.match(/{[^}] }/g)[0])
}

Execution log
11:48:30 PM Notice  Execution started
11:48:29 PM Info    {text inside of brackets}
11:48:31 PM Notice  Execution completed

CodePudding user response:

If you want to use a capture group, then use:

\[\?replaceMe (.*?) \?\]

If you don't want to use a capture group, then use lookarounds:

(?<=\[\?replaceMe ).*?(?= \?\])
  • Related