Remove/Replace all Commas from a String in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
5 min

banner

# Table of Contents

  1. Remove/Replace all Commas from a String in JavaScript
  2. Remove/Replace all Commas from a String using String.replace()
  3. Remove/Replace all Commas from a String using split() and join()
  4. Replace only the first occurrence of a Comma in a string

# Remove/Replace all Commas from a String in JavaScript

Use the String.replaceAll() method with a comma as the first parameter and an empty string as the second to remove all commas from a string.

The replaceAll() method will remove all commas from the string by replacing them with empty strings.

index.js
const str = '1,23,45.67'; const withoutCommas = str.replaceAll(',', ''); console.log(withoutCommas); // ๐Ÿ‘‰๏ธ '12345.67'

remove all commas from string

The code for this article is available on GitHub

If you need to replace all occurrences of a comma in the string, specify a different replacement string.

index.js
const str = 'bobby,hadz,com'; const withoutCommas = str.replaceAll(',', ' '); console.log(withoutCommas); // ๐Ÿ‘‰๏ธ 'bobby hadz com'

replace all commas in string

The code sample replaces each comma in the string with a space.

The String.replaceAll() method returns a new string with all matches of a pattern replaced by the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.
index.js
const str = 'bobby,hadz,com'; const withoutCommas = str.replaceAll(',', ''); console.log(withoutCommas); // ๐Ÿ‘‰๏ธ 'bobbyhadzcom'

The String.replaceAll() method returns a new string with the matches of the pattern replaced. The method doesn't change the original string.

Strings are immutable in JavaScript.

We remove all commas from the string by replacing them with empty strings.

If you have to do this often, extract the logic into a reusable function.

index.js
function removeAllCommas(string) { return string.replaceAll(',', ''); } // ๐Ÿ‘‡๏ธ bobbyhadz console.log(removeAllCommas('bobby,hadz')); // ๐Ÿ‘‡๏ธ bobbyhadzcom console.log(removeAllCommas('bobby,hadz,com'));

extract the logic into reusable function

We defined a reusable function that takes a string as a parameter and removes all the commas from the string.

# Remove/Replace all Commas from a String using String.replace()

Alternatively, you can use the String.replace() method.

index.js
const str = '1,23,45.67'; const withoutCommas = str.replace(/,/g, ''); console.log(withoutCommas); // ๐Ÿ‘‰๏ธ '12345.67'

remove all commas from string using replace

The code for this article is available on GitHub

If you need to replace all commas in the string using replace(), use the following code sample instead.

index.js
const str = 'bobby,hadz,com'; const withoutCommas = str.replace(/,/g, '='); console.log(withoutCommas); // ๐Ÿ‘‰๏ธ 'bobby=hadz=com'

replace all commas in string using replace

The code sample replaces all commas in the string with an equal sign.

The String.replace() method returns a new string with one, some, or all matches of a regular expression replaced with the provided replacement.

The method takes the following parameters:

NameDescription
patternThe pattern to look for in the string. Can be a string or a regular expression.
replacementA string used to replace the substring match by the supplied pattern.

We used the String.replace() method in a similar way to how we used the String.replaceAll() method, but we passed a regular expression to the method instead of a string.

index.js
const str = '1,23,45.67'; const withoutCommas = str.replace(/,/g, ''); console.log(withoutCommas); // ๐Ÿ‘‰๏ธ '12345.67'

The forward slashes / / mark the beginning and end of the regular expression.

We used the g (global) flag at the end of the regex because we wanted to match all commas and not just the first occurrence.

Unless the g flag is specified, the String.replace() method matches only the first occurrence of the pattern in a string.

The second parameter is the replacement for each match. For our purposes, we replace each comma with an empty string.

The String.replace() method doesn't change the original string, so make sure to store the output of calling the method in a variable.

You can extract the logic into a reusable function if you have to do this often.

index.js
function removeAllCommas(string) { return string.replace(/,/g, ''); } // ๐Ÿ‘‡๏ธ bobbyhadz console.log(removeAllCommas('bobby,hadz')); // ๐Ÿ‘‡๏ธ bobbyhadzcom console.log(removeAllCommas('bobby,hadz,com'));

The function takes the string as a parameter and removes all occurrences of a comma from the string.

Alternatively, you can use the String.split() method.

# Remove/Replace all Commas from a String using split() and join()

This is a two-step process:

  1. Use the String.split() method to split the string on each comma.
  2. Use the Array.join() method to join the array into a string without a delimiter.
index.js
const str = 'bobby,hadz,com'; const result = str.split(',').join(''); console.log(result); // ๐Ÿ‘‰๏ธ bobbyhadzcom

remove all commas from string using split and join

The code for this article is available on GitHub

If you need to replace the commas in the string, pass a replacement string to the Array.join() method.

index.js
const str = 'bobby,hadz,com'; const result = str.split(',').join('='); console.log(result); // ๐Ÿ‘‰๏ธ bobby=hadz=com

replace all commas in string using split and join

We used the String.split() method to split the string on all occurrences of a comma.

index.js
const str = 'bobby,hadz,com'; // ๐Ÿ‘‡๏ธ [ 'bobby', 'hadz', 'com' ] console.log(str.split(','));

The String.split() method takes a separator and splits the string into an array on each occurrence of the provided delimiter.

The String.split() method takes the following 2 parameters:

NameDescription
separatorThe pattern describing where each split should occur.
limitAn integer used to specify a limit on the number of substrings to be included in the array.

The last step is to use the Array.join() method to join the array into a string.

index.js
const str = 'bobby,hadz,com'; const result = str.split(',').join(''); console.log(result); // ๐Ÿ‘‰๏ธ bobbyhadzcom

The Array.join() method concatenates all of the elements in an array using a separator.

The only argument the Array.join() method takes is a separator - the string used to separate the elements of the array.

If a value for the separator argument is omitted, the array elements are joined with a comma ,.

If the separator argument is set to an empty string, the array elements are joined without any characters in between them.

You can also extract the logic into a reusable function.

index.js
function removeAllCommas(string) { return string.split(',').join(''); } // ๐Ÿ‘‡๏ธ bobbyhadz console.log(removeAllCommas('bobby,hadz')); // ๐Ÿ‘‡๏ธ bobbyhadzcom console.log(removeAllCommas('bobby,hadz,com'));

The function takes a string as a parameter, removes all commas from the string and returns the result.

Which approach you pick is a matter of personal preference. I'd use the String.replaceAll() method because it is quite direct and intuitive.

# Replace only the first occurrence of a Comma in a string

You can also use the replace() method to replace only the first occurrence of a comma in a string.

index.js
const str = '123,456,789'; const replaceFirst = str.replace(/,/, '.'); console.log(replaceFirst); // ๐Ÿ‘‰๏ธ 123.456,789
The code for this article is available on GitHub

The code sample replaces the first comma in the string with a dot.

The first argument we passed to the replace() method is a regular expression.

The forward slashes / / mark the beginning and end of the regular expression.

All we try to match in our regular expression is a single comma.

By default, the replace method replaces the first occurrence of the matched regular expression with the provided replacement string.

When you need to match an exact character, such as a comma, you can also pass a string argument to the replace() method.

index.js
const str = '123,456,789'; const replaceFirst = str.replace(',', '.'); console.log(replaceFirst); // ๐Ÿ‘‰๏ธ 123.456,789

The code sample replaces the first occurrence of a comma with a dot.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev