views:

191

answers:

3
+3  A: 

Add a question mark after the star.

/[%][{]\s*((.|\n|\r)*?)\s*[}][%]/gm
Anton Hansson
+2  A: 

Firstly, to make a wildcard match non-greedy, just append it with ? (so *? instead of * and +? instead of +).

Secondly, your pattern can be simplified in a number of ways.

/%\{\s*([\s\S]*?)\s*\}%/gm

There's no need to put a single character in square brackets.

Lastly the expression in the middle you want to capture, you'll note I put [\s\S]. That comes from Matching newlines in JavaScript as a replacement for the DOTALL behaviour.

cletus
Any particular reason this was downvoted?
cletus
*I* certainly don't see any reason for a downvote. Simply adding the question mark as Anton did may have solved the immediate problem, but this answer is much better.
Alan Moore
+1  A: 

Shorter and faster working:

/%\{([^}]*)\}%/gm
Qwerty
Thanks, I wish I could upvote!
JamesBrownIsDead
That's not quite as broad. It's simple and fast but won't cover cases like `%{some}test}%` that the other answers will. Whether or not that's better or worse depends on an assumption the OP hasn't specified however.
cletus