Avatar of Drevo
Drevo
Flag for United States of America

asked on 

Regular expression to match text within brackets - unclosed bracket woes

I'm having trouble with the regex to find text in a string that is contained within brackets. The following works fine, except it's picking up unclosed bracketed text. I don't want it to match to anything that doesn't have a closing bracket.

Here's an example string:
[Match1] Here is some text. [Don't match this. [Match2]

I'm using the following regex:
/\[.*?\]/g

Open in new window


My regex is matching on the bracket in "[Don't match this". Can you help with correcting the Regex for this situation?

Here's the example on regex101.
Regular Expressions

Avatar of undefined
Last Comment
Drevo

8/22/2022 - Mon