-1

I am trying to write a JavaScript RegEx which returns content(maybe including nested parentheses) which is between parentheses like the solution described in this article (implemented by Java RegEx)

String:

xxx
@connect(({ app }) => (
  {
    ...app
  }
))
xxx

Result:

({ app }) => (
  {
    ...app
  }
)

Is it possible to resolve it with JavaScript RegEx?

licaomeng
  • 723
  • 1
  • 8
  • 19
  • So, what have you tried till now? Where are you stuck, what is the regex you are currently using? Currently you have a 'yes'/'no' question (is it possible ...) – Icepickle Feb 12 '19 at 16:50
  • Is this for an infinite depth of nested `( )` – JGNI Feb 12 '19 at 16:52
  • @Icepickle The solution in http://www.drregex.com/2017/11/match-nested-brackets-with-regex-new.html implemented with Java RegEx, not applicable to JavaScript. So I ask 'Is it possible...' – licaomeng Feb 12 '19 at 17:00
  • @JGNI Just for finite depth :-) – licaomeng Feb 12 '19 at 17:03
  • It's not possible. JavaScript does not support forward references in the way Java/PCRE do. – wp78de Feb 12 '19 at 21:46
  • @wp78de Yes, mentioned in that article – licaomeng Feb 14 '19 at 06:15
  • Exactly. So, what's the point of this question if you already knew? If so, you should have pointed that out. Or haven't you read the entire article before you asked. – wp78de Feb 14 '19 at 17:37

1 Answers1

0

You can use the following expression:

/                     // start expression
  (?<=@connect\()     // positive lookbehind
  \(                  // open parenthesis
  (.|[\n\r])+         // anything, including new-lines
  \)                  // closed parenthesis
  (?=\)$)             // positive lookahead
/gm                   // global match, multiline

var re = /(?<=@connect\()\((.|[\n\r])+\)(?=\)$)/gm;
var s = `xxx
@connect(({ app }) => (
  {
    ...app
  }
))
xxx`

console.log(s.match(re)[0]);
Mr. Polywhirl
  • 31,606
  • 11
  • 65
  • 114