Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
44
rated 0 times [  45] [ 1]  / answers: 1 / hits: 29758  / 10 Years ago, sat, january 24, 2015, 12:00:00

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


More From » regex

 Answers
9

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:



({$[^$]+$})


RegEx Demo


[#68100] Thursday, January 22, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darrell

Total Points: 109
Total Questions: 113
Total Answers: 113

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
;