5

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.

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

expected

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

5
  • 8
    str.replace(/,/g, '') Commented Mar 3, 2014 at 11:40
  • 1
    Can also do str.replace(',', '', 'g'). From the docs: "To perform a global search and replace, either include the g switch in the regular expression or if the first parameter is a string, include g in the flags parameter." Commented Mar 3, 2014 at 11:41
  • i tried this global option as mentioned not working for me :( Commented Mar 3, 2014 at 11:42
  • jsfiddle.net/BbNeK Commented Mar 3, 2014 at 11:43
  • Thanks Andolasoft....my bad i was using single coma for regex str=str.replace('/,/g',''); Commented Mar 3, 2014 at 11:44

5 Answers 5

4

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

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.

//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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

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

3 Comments

The global flag used in that way will work fine in Firefox, but not across other browsers as it is non-standard. Please see the following answer: stackoverflow.com/questions/4981501/…
That's what I get for skimming documentation. Good to know. Going to delete my previous comment since it's now redundant.
I can see why they introduced it, but it doesn't seem to have caught on. No worries and thanks for the upvote :)
2

Try this code,

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

Comments

1

Try this

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

DEMO

Comments

-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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.