Last updated: Mar 1, 2024
Reading timeยท5 min
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.
const str = '1,23,45.67'; const withoutCommas = str.replaceAll(',', ''); console.log(withoutCommas); // ๐๏ธ '12345.67'
If you need to replace all occurrences of a comma in the string, specify a different replacement string.
const str = 'bobby,hadz,com'; const withoutCommas = str.replaceAll(',', ' '); console.log(withoutCommas); // ๐๏ธ 'bobby hadz com'
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:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A string used to replace the substring match by the supplied pattern. |
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.
If you have to do this often, extract the logic into a reusable function.
function removeAllCommas(string) { return string.replaceAll(',', ''); } // ๐๏ธ bobbyhadz console.log(removeAllCommas('bobby,hadz')); // ๐๏ธ bobbyhadzcom console.log(removeAllCommas('bobby,hadz,com'));
We defined a reusable function that takes a string as a parameter and removes all the commas from the string.
Alternatively, you can use the String.replace()
method.
const str = '1,23,45.67'; const withoutCommas = str.replace(/,/g, ''); console.log(withoutCommas); // ๐๏ธ '12345.67'
If you need to replace all commas in the string using replace()
, use the
following code sample instead.
const str = 'bobby,hadz,com'; const withoutCommas = str.replace(/,/g, '='); console.log(withoutCommas); // ๐๏ธ 'bobby=hadz=com'
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:
Name | Description |
---|---|
pattern | The pattern to look for in the string. Can be a string or a regular expression. |
replacement | A 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.
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.
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.
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.
This is a two-step process:
String.split()
method to split the string on each comma.Array.join()
method to join the array into a string without a
delimiter.const str = 'bobby,hadz,com'; const result = str.split(',').join(''); console.log(result); // ๐๏ธ bobbyhadzcom
If you need to replace the commas in the string, pass a replacement string to
the Array.join()
method.
const str = 'bobby,hadz,com'; const result = str.split(',').join('='); console.log(result); // ๐๏ธ bobby=hadz=com
We used the String.split()
method to split the string on all occurrences of a
comma.
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:
Name | Description |
---|---|
separator | The pattern describing where each split should occur. |
limit | An 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.
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 ,
.
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.
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.
You can also use the replace()
method to replace only the first occurrence of
a comma in a string.
const str = '123,456,789'; const replaceFirst = str.replace(/,/, '.'); console.log(replaceFirst); // ๐๏ธ 123.456,789
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.
/ /
mark the beginning and end of the regular expression.All we try to match in our regular expression is a single comma.
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.
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.
You can learn more about the related topics by checking out the following tutorials: