I want to fetch text surrounded by some customized “brackets”, i.e.: {{…}} or @(…)@
when the brackets is only one character length (i.e.: {..}) , it is :
/{(.*?)}/g
and there is a more efficient solution, using “not-end-bracket” pattern:
/{([^}]*)}/g
but for two-characsters brackets, only this works
/{{(.*?)}}/g
I can’t find an efficient solution like the one in the one-character case.
Is there any?
I’m assuming you don’t need to worry about nesting. If you doo, you shouldn’t be using regular expressions.
Here’s a way to do what you want without using non-greedy matching (
*?):In English:
This generalizes out to n-curly braces by having n separate branches, one for non-curly, one for a curly and a non-curly, one for two curlies and a non-curly, etc.
In your special case you could just write:
BTW: the group probably doesn’t need to be capturing. eg: