Remove the last Comma from a String in JavaScript

avatar

Borislav Hadzhiev

Thu Dec 02 20214 min read

Remove the last Comma from a String #

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.

index.js
const str1 = 'apple,banana,kiwi,'; const withoutLastComma = str1.replace(/,*$/, ''); console.log(withoutLastComma); // ๐Ÿ‘‰๏ธ "apple,banana,kiwi"
If you're looking to avoid using regular expression, check out the next code snippet.

We passed the following 2 parameters to the String.replace method:

  1. a regular expression we want to match in the string
  2. the replacement for each match - in our case an empty string, because we're looking to remove any trailing commas

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.

index.js
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.

index.js
const str1 = 'apple,banana,kiwi'; const withoutLastComma = str1.replace(/,*$/, ''); console.log(withoutLastComma); // ๐Ÿ‘‰๏ธ "apple,banana,kiwi"
The 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.

Remove the last Comma from a String using endsWith #

To remove the last comma from a string:

  1. Call the endsWith() method to check if the string ends with a comma.
  2. If it does, use the slice method to get a portion of the string that excludes the last comma.
  3. If it doesn't, return the string as is.
index.js
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. start index - the index (zero-based) of the first character that should be included in the new string.
  2. end index - extract characters up to, but not including this index. A negative index of -1 means go up to, but not including the last character of the string
Passing an end index parameter of -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 the lastIndexOf method instead.

Remove the last Comma from a String using lastIndexOf #

To remove the last comma from a string:

  1. Call the lastIndexOf() method to check if the last character in the string is a comma.
  2. If it is, use the slice method to get a portion of the string that excludes the last comma.
  3. If it doesn't, return the string as is.
index.js
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.

Indexes are zero-based in JavaScript, meaning the index of the first character in a string is 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.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee