Removing All Commas From a String in JavaScript

So you need to remove commas from a string in JavaScript? These are the two best ways to achieve this.

Published Categorized as Coding, JavaScript

Suppose you have a string with comma-separated values. And, for one reason or another, you want to remove the commas from that string.

What’s the best way to accomplish this?

To remove all commas in a string in JavaScript, use the String.replaceAll() method or String.replace() with RegEx to replace the comma character with an empty value.

The String.replace() method expects two parameters, one for a pattern and one for a replacement:

// Syntax for String.replace()
String.replace(pattern, replacement);

And so does the String.replaceAll() method:

// Syntax for String.replaceAll()
String.replaceAll(pattern, replacement);

For both methods:

  • The pattern can be a string or a regular expression (RegEx);
  • The replacement can be a string or a function to be called when a match is found.

String.replace() is supported by all modern and legacy browsers. In contrast, String.replaceAll() isn’t.

If legacy browser support is a non-negotiable, the good news is that you can substitute the String.replaceAll() method with the String.replace() method with a RegEx pattern.

I’ll show you how below.

How It Works

Let’s assume that, in our JavaScript code, there’s a string called groceryList where we’ve stored apples, bananas, and oranges separated by commas and blank spaces.

// Store apples, bananas, oranges in groceryList
var groceryList = 'apples, bananas, oranges';

Remove First Comma in String

To remove the first comma in a string in JavaScript, use the String.replace() method to pass the comma as a parameter in a string and replace with an empty value (”).

// Store apples, bananas, oranges in groceryList
var groceryList = 'apples, bananas, oranges';

// Remove the first comma in groceryList
var groceryList = groceryList.replace(',','');

// Log updated groceryList to the console
console.log(groceryList); // apples bananas, oranges

Try this out by copy/pasting the above code into your browser’s console and hitting “Enter.” You will observe the following result:

Snapshot from the console

Remove All Commas in a String

With String.replaceAll()

The simplest way to remove all commas in a string in JavaScript is to use the String.replaceAll() method.

// Store apples, bananas, oranges in groceryList
var groceryList = 'apples, bananas, oranges';

// Remove all commas in groceryList
var groceryList = groceryList.replaceAll(',','');

// Log updated groceryList to the console
console.log(groceryList); // apples bananas oranges

Give the code above a try in your browser’s console, and this is what you’ll see:

Snapshot from the console

This method is quick and computationally efficient, as it uses a built-in JavaScript method. But, as I already touched on, it isn’t supported by most legacy browsers.

If legacy browser support is of the essence, consider opting for the solution below instead.

With String.replace()

There are two ways to pass the pattern, the first of the two required parameters, to the String.replace() method.

The first way, which we already covered, is to pass on the pattern as a string. But, if you do so, the method will generate a new string with only the first comma removed.

The second way, which we’re interested in here, is to pass on the pattern as RegEx. A RegEx rule will allow you to target all the commas in your string and replace them with an empty value.

// Store apples, bananas, oranges in groceryList
var groceryList = 'apples, bananas, oranges';

// Remove all commas in groceryList
var groceryList = groceryList.replace(/[,]/g,'');

// Log updated groceryList to the console
console.log(groceryList); // apples bananas oranges

Notice the g at the end of our regular expression. It stands for global, and it makes the pattern applicable to all instances of the quotation symbol, not just the first one. (Without it, only the first quotes will get removed.)

Run this code in your browser’s console, and you will see that it returns the same, comma-free string as the previous one:

Snapshot from the console

Things to Know

You can use the same two methods to replace all commas with a different separator, be it semicolon or any other character you need:

// Store apples, bananas, oranges in groceryList
var groceryList = 'apples, bananas, oranges';

// Replace all commas with semicolons
var groceryList = groceryList.replace(/[,]/g,';');

// Log updated groceryList to the console
console.log(groceryList); // apples; bananas; oranges

Similarly, if the words in your string were separated by comma but not by whitespace, you could replace the comma with comma and whitespace for readability purposes:

// Store apples, bananas, oranges in groceryList
var groceryList = 'apples,bananas,oranges';

// Replace all commas with semicolons
var groceryList = groceryList.replace(/[,]/g,', ');

// Log updated groceryList to the console
console.log(groceryList); // apples, bananas, oranges

In Conclusion

Thank you for reading this far! I hope this post helped you figure out how to remove commas from a string in JavaScript. Or, if the requirement is different, how to replace them with something else.

If you have any questions, or you’d like to share solutions of your own with the rest of this post’s readers, be sure to leave a reply below.

Leave a comment

Your email address will not be published.