How to Remove or Replace All Commas from a String in JavaScript
Removing or replacing all commas in a string is a common data cleaning task. You might need to parse a number formatted with thousands separators ("1,234,567"), clean up user input, or reformat a comma-separated list. Modern JavaScript provides a simple and highly readable method for this: String.prototype.replaceAll().
This guide will show you how to use replaceAll() for this task, explain the classic replace() method with a regular expression, and clarify how to replace only the first comma if needed.
The Core Method (Recommended): String.prototype.replaceAll()
The replaceAll() method is the most direct and modern way to replace every occurrence of a substring. It is easy to read and clearly expresses your intent.
Problem: you have a string with commas that you want to replace with another character, or remove entirely.
// Problem: How to replace all commas in this string?
const formattedNumber = '1,234,567';
Solution: provide the comma character (',') as the first argument and your replacement character as the second. To remove the commas, use an empty string ('').
const formattedNumber = '1,234,567';
// Replace all commas with an empty string
const cleanNumber = formattedNumber.replaceAll(',', '');
console.log(cleanNumber); // Output: '1234567'
// Replace all commas with spaces
const withSpaces = formattedNumber.replaceAll(',', ' ');
console.log(withSpaces); // Output: '1 234 567'
Important: Strings are immutable in JavaScript. replaceAll() does not change the original string; it returns a new string with the replacements.
The Classic Method: replace() with a Regular Expression
Before replaceAll() was introduced, the standard way to replace all occurrences was to use the replace() method with a regular expression that had the global (g) flag. This method is still very common and useful.
Solution:
const formattedNumber = '1,234,567';
// The regex /,/g finds all comma characters globally.
const cleanNumber = formattedNumber.replace(/,/g, '');
console.log(cleanNumber); // Output: '1234567'
While this works perfectly, replaceAll() is now the preferred method for simple, literal string replacements because it is more explicit and doesn't require knowledge of regular expressions.
How to Replace Only the First Comma
If you only want to replace the first occurrence of a comma, you should use the standard replace() method without the global flag. When given a string as the first argument, replace() will only affect the first match it finds.
Problem: you want to replace only the first comma in a string.
// Problem: Replace only the first comma.
const str = 'a,b,c';
Solution:
const str = 'a,b,c';
const newStr = str.replace(',', '-');
console.log(newStr); // Output: 'a-b,c'
This is the correct and most direct way to replace just the first instance.
Conclusion
JavaScript's string methods provide simple and powerful tools for replacing characters.
- To replace all occurrences of a comma, the
string.replaceAll(',', '...')method is the recommended best practice. It is modern, readable, and explicit. - The classic
string.replace(/,/g, '...')method is a powerful alternative that achieves the same result. - To replace only the first occurrence of a comma, use the standard
string.replace(',', '...')method without the global regex flag.