0

Title kinda says it all. This is what I have right now:

const str = "(((111)00)(01)(02)))))";
const rgx = /\([^()]*\)/g;
str.match(rgx).forEach(function(m) {
  document.body.insertAdjacentHTML('beforeend', m + '<br>');
});

Mine will get all nested parentheses if they are directly inside of the outer parentheses. However it will not work if I nest parentheses within another nested parentheses (Inception style).

Right now for the string "(((111)00)(01)(02))" it returns (111), (01), (02) in my example. I would like it to return (111), ((111)00), (01), (02).

Really my final goal is to check for balance and return true or false depending on the result.

Any help is appreciated. Thanks!

Barmar
  • 741,623
  • 53
  • 500
  • 612
tripstar
  • 161
  • 1
  • 2
  • 11
  • 3
    Pedantry: The movie Inception's title means to create an idea in someone's mind through their dreams so they thought they came up with the idea themselves - it has nothing to do with recursion or reentrancy. – Dai Jul 17 '18 at 20:45
  • 1
    Maybe someone is creating parenthesis inside of parenthesis without his knowledge which is why he wants regex to catch the parenthetical inception – DannyMoshe Jul 17 '18 at 20:47
  • since you loop results anyway, i would use two simpler regexps and concat them before filtering or foreaching – dandavis Jul 17 '18 at 20:50
  • Why not increment an integer each time you find a `(` and decrement it whenever you find `)`? If your integer ends at 0 your parentheses are balanced. – emsimpson92 Jul 17 '18 at 23:47

0 Answers0