Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要从变量中获取所有加号并用空格替换它们。我试过了:
someVariable = "0+123+45+6"; someVariable.replace(/+/g, ' ');
但这不起作用......这种情况的正确语法是什么?
the+是一个特殊的字符(一个或多个),所以你需要转义它。
+
应该/\+/g
/\+/g
EDIT 正则表达式不会修改字符串对象本身。但返回结果。
someVariable = "0+123+45+6"; someVariable = someVariable.replace(/\+/g, ' ');