I’m working on a regex pattern to match nested parentheses with exponential depth. While I have a working solution for shallow nesting, it fails when the nesting depth becomes very deep.
Here’s my current regex pattern:
const regex = /(([^()]+))/;
const text = "(((A))) (((((B))))) (((((((C)))))))";
const matches = text.match(regex);
console.log(matches);
In this example, I want to match the innermost content within the deepest set of parentheses, like "C". However, my current regex code only matches "A" and "B."
I’m looking for a regex pattern that can handle matching the innermost content within deeply nested parentheses like the "C" in the example. Any insights or corrections to my regex code would be greatly appreciated!
2
Answers
You forgot the global modifier,
/g
You can avoid parenthesis in your capture result and the need for capture groups with a few lookarounds:
Depending on the complexity of your input, you could easily run into issues. For example, is the expectation that
D
, notC
, gets matched? Whoops…Seems not possible with 1 regexp but you could match opening parentheses + content and check whether the closing parentheses match: