开发者

js replace global doesn't like +?

开发者 https://www.devze.com 2023-01-07 08:26 出处:网络
So I\'ve been playing around with the replace function (开发者_JAVA百科method?) in js. $get(\'msgBar\').replace(/+/g,\' \');

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

精彩评论

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