atars
atars

Reputation: 1

Problem with newline in JavaScript regexp

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?

Upvotes: 0

Views: 235

Answers (1)

kennytm
kennytm

Reputation: 523184

} 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.

Upvotes: 6

Related Questions