且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将所有加号(+)替换为字符串中的空格

更新时间:2022-04-04 22:29:01

+ 字符在正则表达式中具有特殊意义。这是一个量词,意思是一个或多个上一个字符,字符类或组的内容。

The + character has special significance in regular expressions. It's a quantifier meaning one or more of the previous character, character class, or group.

您需要转义 + ,如下所示:

You need to escape the +, like this:

i.replace(new RegExp("\\+","g"),' ')...

或者更简单地说,通过使用预编译表达式:

Or more simply, by using a precompiled expression:

i.replace(/\+/g,' ')...