I’d like to know the regex that matches everything inside {}. The string might contain nested {}; I don’t care about them.
Example string: {Don't} trust me. I'm a {very very {evil}} good guy.
I want the result to be Don't and very very {evil}.
The only regex I have at the moment is /{\w+}/ which only captures evil. It returns Dont also if it doesn’t have the apostrophe.
Since you are having nested braces, so the regex to extract the parts between the braces is not as straight as: –
\{.+?\}. This regex will stop at the first}it finds. So, it will not behave nicely for nested braces. For that, you need a slightly more complex regex. However, I would suggest not to use Regex for such kind of problems. Regex is not good enough to consider matching pairs of brackets. They can only parse regular language. For anything higher than that, you should write your own parser.That being said, you can try this regex: –
And get the
group 1. This will consider your nested braces, provided they are balanced. And you don’t have your braces as a part of actual string.Apart from the above solution, you can have a look at this sample parser, which would work for your particular case. In fact this should work for any form of nested braces, provided you have balanced braces: –
Output: –