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.
DrevoAsked:
Who is Participating?
 
Terry WoodsIT GuruCommented:
This is what you want I believe:

\[[^\[]*?\]

Open in new window


View in regex101
0
 
DrevoAuthor Commented:
Awesome! That's it! Thank you!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.