简体   繁体   English

如何在JavaScript中替换加号?

[英]How can I replace a plus sign in JavaScript?

I need to make a replace of a plus sign in a javascript string. 我需要在javascript字符串中替换加号。 there might be multiple occurrence of the plus sign so I did this up until now: 可能会出现多次加号,所以我直到现在才这样做:

myString= myString.replace(/+/g, "");#

This is however breaking up my javascript and causing glitches. 然而,这会破坏我的javascript并导致故障。 How do you escape a '+' sign in a regular expression? 你如何逃避正则表达式中的“+”符号?

myString = myString.replace(/\+/g, "");

You need to escape the + as its a meta char as follows: 您需要将+转义为元字符,如下所示:

myString= myString.replace(/\+/g, "");

Once escaped, + will be treated literally and not as a meta char. 一旦转义, +将按字面处理,而不是作为元字符处理。

我更喜欢这个:

myString.replace(/[+]/g, '').

你应该逃避你的+号, \\+

myString.replace(/\+/g, "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM