Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Regex: Make ungreedy

I have this regex which looks for %{any charactering including new lines}%:

/[%][{]\s*((.|\n|\r)*)\s*[}][%]/gm

If I test the regex on a string like "%{hey}%", the regex returns "hey" as a match.

However, if I give it "%{hey}%%{there}%", it doesn't match both "hey" and "there" seperately, it has one match—"hey}%%{there".

How do I make it ungreedy to so it returns a match for each %{}%?

like image 539
JamesBrownIsDead Avatar asked Sep 11 '25 22:09

JamesBrownIsDead


1 Answers

Add a question mark after the star.

/[%][{]\s*((.|\n|\r)*?)\s*[}][%]/gm
like image 91
Anton Hansson Avatar answered Sep 14 '25 10:09

Anton Hansson