I want to find a specific pattern inside a string.
The pattern: (\{\$.+\$\})
Example matche: {$ test $}
The problem I have is when the text has 2 matches on the same line. It returns one match. Example: this is a {$ test $} content {$ another test $}
This returns 1 match: {$ test $} content {$ another test $}
It should returns 2 matches:
{$ test $}
and {$ another test $}
Note: I'm using Javascript
Problem is that your regex (\{\$.+\$\})
is greedy in nature when you use .+
that's why it matches longest match between {$
and }$
.
To fix the problem make your regex non-greedy:
(\{\$.+?\$\})
Or even better use negation regex:
(\{\$[^$]+\$\})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With