将所有加号(+)替换为字符串中的空格 [英] Replace all plus signs (+) with space in a string
本文介绍了将所有加号(+)替换为字符串中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不确定如何在正则表达式中转义'+'。 Plus可以在 i
中多次出现,因此我们需要替换字符串中的所有 +
。这就是我所拥有的:
I'm not sure how to escape '+' in regex. Plus can come multiple times in i
so we need to replace all +
in the string. Here's what I have:
i.replace(new RegExp("+","g"),' ').replace(new RegExp("selectbasic=","g"),'').split('&');
但是这给了我这个错误:
But this gives me this error:
Uncaught SyntaxError:无效的正则表达式:/ + /:无需重复
Uncaught SyntaxError: Invalid regular expression: /+/: Nothing to repeat
推荐答案
+
字符在正则表达式中具有特殊意义。这是一个量词,意思是一个或多个上一个字符,字符类或组的内容。
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,' ')...
这篇关于将所有加号(+)替换为字符串中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文