简体   繁体   English

JavaScript正则表达式中的换行符问题

[英]Problem with newline in JavaScript regexp

i tried to do not show "SPAM" in string below using that regex: 我试图不使用该正则表达式在下面的字符串中显示“ SPAM”:

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

What i was supposed to see was "~1~ ~2~" 我应该看到的是“〜1〜〜2〜”

(or something like that) but i got just ~2~. (或类似的东西),但我只有〜2〜。 Why? 为什么?

} and { are also elements of the character class [\\s\\S] . }{也是字符类[\\s\\S]元素。 You should avoid matching this by: 您应该通过以下方式避免与之匹配:

/{[^}]+}/g

so that the regex stops once the } is found. 这样,一旦找到} ,则正则表达式就会停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM