Borislav Hadzhiev
Last updated: Dec 2, 2021
Check out my new book
To remove the last comma from a string, call the replace()
method with the
following regular expression /,*$/
as the first parameter and an empty string
as the second. The replace
method will return a new string with the last comma
removed.
const str1 = 'apple,banana,kiwi,'; const withoutLastComma = str1.replace(/,*$/, ''); console.log(withoutLastComma); // 👉️ "apple,banana,kiwi"
We passed the following 2 parameters to the String.replace method:
The forward slashes / /
mark the beginning and end of the regular expression.
The asterisk *
matches the preceding item (the comma) 0
or more times.
And the dollar $
sign matches the end of the input.
In it's entirety, the regular expression matches 0
or more commas at the end
of the string.
If you need help reading a regular expression, check out this regex cheatsheet by the MDN docs, it has served me well over the years.
Note that this approach would also work if the string ends with multiple commas.
const str1 = 'apple,banana,kiwi,,,,,,'; const withoutLastComma = str1.replace(/,*$/, ''); console.log(withoutLastComma); // 👉️ "apple,banana,kiwi"
If the string doesn't end with a comma (the regular expression doesn't match
anything), the replace
method will return a copy of the entire string.
const str1 = 'apple,banana,kiwi'; const withoutLastComma = str1.replace(/,*$/, ''); console.log(withoutLastComma); // 👉️ "apple,banana,kiwi"
replace
method does not change the original string, it returns a new string with one or more matches replaced. Strings are immutable in JavaScript.An alternative approach is to use the String.endsWith method.
To remove the last comma from a string:
endsWith()
method to check if the string ends with a comma.slice
method to get a portion of the string that
excludes the last comma.const str2 = 'avocado,grapefruit,orange'; const withoutLastComma = str2.endsWith(',') ? str2.slice(0, -1) : str2; // 👇️ "avocado,grapefruit,orange" console.log(withoutLastComma);
The endsWith
method allows us to check if the string ends with a specific
substring.
We used a ternary operator, which is very similar to an if/else statement.
If the string ends with a comma, we use the slice method to get a new string with the last character chopped of.
If the string doesn't end with a comma, we return it as is.
We passed the following parameters to the String.slice method:
-1
means go up to, but not including the last character
of the string-1
and str.length - 1
is the same. We instruct the slice
method to go up to, but not including the last character in the string.This approach only removes the last comma from the string, it doesn't handle a scenario where the string ends with multiple commas. If you need to handle that, use the regex approach instead.
Note that the
String.endsWith
method is not supported in Internet Explorer. If you have to support the browser, use thelastIndexOf
method instead.
To remove the last comma from a string:
lastIndexOf()
method to check if the last character in the string
is a comma.slice
method to get a portion of the string that excludes
the last comma.const str3 = 'one,two,three,'; const withoutLastComma = str3.lastIndexOf(',') === str3.length - 1 ? str3.slice(0, -1) : str3; console.log(withoutLastComma); // 👉️ "one,two,three"
This code snippet is very similar to the previous one, however this time we used the String.lastIndexOf method.
The method returns the index of the last occurrence of a substring in a string.
0
and the index of the last - string.length - 1
.The next step is to subtract 1
from the string's length to get the value of
the last index in the string and compare it to the value the lastIndexOf
method returned.
If the string ends with a comma, we remove it using the slice
method,
otherwise we return the string as is.