0

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?

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.