开发者

js replace global doesn't like +?

So I've been playing around with the replace function (开发者_JAVA百科method?) in js.

$get('msgBar').replace(/+/g,' ');

Let's say the content of $get('msgBar') is "Welcome+back+now".

replace('+',' ') would only replace the first +, and not the second.

replace(/+/g,' ') crashes

replace(/"+"/g,' ') and replace(/\+/g,' ') are both the same as the first

I'm sure the solution is easy... :)


You must quote +:

$get('msgBar').replace(/\+/g,' ');

'+' is a meta character, like '*'. It means "one more repetitions". It you literally want '+' , then you have to quote it with the backslash.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜