I am new to the regex and trying to write the RegEx for the below string.
[1] [2] [3] [] [5[any]] my text
I want something like
1
2
3
5[any]
my text
I have tried the /\[(.*?)\]/g but it not working with inner brackets & the last word outside of the brackets
https://regex101.com/r/NYvxMC/2
>Solution :
Try:
((?<=\[)(?:[^\[\]]|\[[^\[\]]+\])*(?=\])|(?<=\s)[\w\h:.,]+$)
See: regex101
Explanation
( ... ): capture result to group 1:(?<=\[) ... (?=\]): either in between brackets:(?: ... )*: match 0 or more times:[^\[\]]: either non brackets
|: or\[ ... \]: brackets with[^\[\]]+: non-brackets in between
|: or words at the end:(?<=\s): check that a space is preceeding[\w\h:.,]+: match word characters and horizontal space; here you can add more characters as needed