views:

31

answers:

1

Hi, i tried to do not show "SPAM" in string below using that regex:

alert("{SPAM\nSPAM} _1_ {SPAM} _2_".replace(/{[\s\S]+}/gm, ""));

What i was supposed to see was "~1~ ~2~"

(or something like that) but i got just ~2~. Why?

+6  A: 

} and { are also elements of the character class [\s\S]. You should avoid matching this by:

/{[^}]+}/g

so that the regex stops once the } is found.

KennyTM
Was about to post something about (un)greedy, but this is definitely better.
Wrikken