JavaScript替换字符串中的所有逗号

时间:2022-06-12 16:51:49

I want to convert all comma in below string to space or say blank, i tried below code which is only taking care of first comma, I tried global indicator /g as well but that do nothing.

我想将下面的字符串中的所有逗号转换为空格或说空白,我尝试下面的代码只处理第一个逗号,我尝试了全局指标/ g但是什么都不做。

What I am doing wrong?

我做错了什么?

var str="D'Or, Megan#LastName Jr., FirstName#BMW, somename#What, new";
str=str.replace(',','');
alert(str)

Output

D'Or Megan#LastName Jr., FirstName#BMW, somename#What, new

D'Or Megan#LastName Jr.,FirstName#BMW,somename#What,new

expected

D'Or Megan#LastName Jr. FirstName#BMW somename#What new

D'Or Megan#LastName Jr. FirstName#BMW somename#新的

5 个解决方案

#1


4  

To replace any given string u need to use regular expressions. You need to use a RegExp Object to ensure crossbrowser compatibility.

要替换任何给定的字符串,您需要使用正则表达式。您需要使用RegExp对象来确保crossbrowser兼容性。

The use of the flags parameter in the String.replace method is non-standard. For cross-browser compatibility, use a RegExp object with corresponding flags.

String.replace方法中flags参数的使用是非标准的。对于跨浏览器兼容性,请使用带有相应标志的RegExp对象。

//Init
var str = "D'Or, Megan#LastName Jr., FirstName#BMW, somename#What, new";
var regex = new RegExp(',', 'g');

//replace via regex
str = str.replace(regex, '');

//output check
console.log(str); // D'Or Megan#LastName Jr. FirstName#BMW somename#What new

See that fiddle: http://jsfiddle.net/m1yhwL3n/1/ example. Thats how it will work fine for all browsers.

看到那个小提琴:http://jsfiddle.net/m1yhwL3n/1/的例子。这就是它如何适用于所有浏览器。

#2


2  

You need to use the global option in the following way:

您需要以下列方式使用全局选项:

str=str.replace(/,/g,'');

#3


1  

Try this

var str="D'Or, Megan#LastName Jr., FirstName#BMW, somename#What, new";
str=str.replace(/,/g ,'');
alert(str)

DEMO

#4


1  

Try this code,

试试这个代码,

var str="D'Or, Megan#LastName Jr., FirstName#BMW, somename#What, new";
val=str.replace(/,/g, '');
alert(val);

#5


-1  

My Bad i was using comma for regex

我的坏我正在使用逗号进行正则表达式

wrong str=str.replace('/,/g','');

good str=str.replace(/,/g,'');

Thanks all for correct this..I will add points for all of you. :)

谢谢大家的正确答案..我会为你们所有人加分。 :)

#1


4  

To replace any given string u need to use regular expressions. You need to use a RegExp Object to ensure crossbrowser compatibility.

要替换任何给定的字符串,您需要使用正则表达式。您需要使用RegExp对象来确保crossbrowser兼容性。

The use of the flags parameter in the String.replace method is non-standard. For cross-browser compatibility, use a RegExp object with corresponding flags.

String.replace方法中flags参数的使用是非标准的。对于跨浏览器兼容性,请使用带有相应标志的RegExp对象。

//Init
var str = "D'Or, Megan#LastName Jr., FirstName#BMW, somename#What, new";
var regex = new RegExp(',', 'g');

//replace via regex
str = str.replace(regex, '');

//output check
console.log(str); // D'Or Megan#LastName Jr. FirstName#BMW somename#What new

See that fiddle: http://jsfiddle.net/m1yhwL3n/1/ example. Thats how it will work fine for all browsers.

看到那个小提琴:http://jsfiddle.net/m1yhwL3n/1/的例子。这就是它如何适用于所有浏览器。

#2


2  

You need to use the global option in the following way:

您需要以下列方式使用全局选项:

str=str.replace(/,/g,'');

#3


1  

Try this

var str="D'Or, Megan#LastName Jr., FirstName#BMW, somename#What, new";
str=str.replace(/,/g ,'');
alert(str)

DEMO

#4


1  

Try this code,

试试这个代码,

var str="D'Or, Megan#LastName Jr., FirstName#BMW, somename#What, new";
val=str.replace(/,/g, '');
alert(val);

#5


-1  

My Bad i was using comma for regex

我的坏我正在使用逗号进行正则表达式

wrong str=str.replace('/,/g','');

good str=str.replace(/,/g,'');

Thanks all for correct this..I will add points for all of you. :)

谢谢大家的正确答案..我会为你们所有人加分。 :)