Remove the Leading and Trailing Comma from a String in JS

avatar

Borislav Hadzhiev

Last updated: Jul 24, 2022

banner

Photo from Unsplash

Remove the Leading and Trailing Comma from a String #

To remove the leading and trailing comma from a string, call the replace() method with the following regular expression as the first parameter - /(^,)|(,$)/g and an empty string as the second. The method will return a copy of the string without the leading or trailing comma.

index.js
const str = ',apple,banana,kiwi,grapefruit,'; const removed = str.replace(/(^,)|(,$)/g, ''); // 👇️ "apple,banana,kiwi,grapefruit" console.log(removed);
If you're looking to avoid using regular expressions, scroll down to the next code snippet.

We used the String.replace method to remove the leading and trailing commas from the string.

We passed the following arguments to the replace method:

  1. a regular expression to match in the string
  2. the replacement for each match. (empty string to remove the leading and trailing commas)

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

The caret ^ matches the beginning of the input and the dollar sign $ matches the end of the input.

We used the pipe | (OR) special character to match a comma in the beginning or a comma in the end of the string.

We used the g (global) flag because we want to match not just the first occurrence (the comma in the beginning), but also the comma in the end.

If you ever need help reading a regular expression, check out this regex cheatsheet from MDN.

If the string contains no leading or trailing comma, the replace method returns a copy of the string.

An alternative approach is to use the startsWith and endsWith methods to check if the string starts with and ends with a comma.

index.js
function removeCommas(str) { if (str.startsWith(',') && str.endsWith(',')) { return str.slice(1, -1); } if (str.startsWith(',')) { return str.slice(1); } if (str.endsWith(',')) { return str.slice(0, -1); } return str; } // 👇️ "apple,banana,kiwi,grapefruit" console.log(removeCommas(',apple,banana,kiwi,grapefruit,')); // 👇️ "apple,banana,kiwi,grapefruit" console.log(removeCommas('apple,banana,kiwi,grapefruit,')); // 👇️ "apple,banana,kiwi,grapefruit" console.log(removeCommas(',apple,banana,kiwi,grapefruit')); // 👇️ "apple,banana,kiwi,grapefruit" console.log(removeCommas('apple,banana,kiwi,grapefruit'));

We handle multiple scenarios:

  1. The string contains both a leading and a trailing comma
  2. The string contains only a leading comma
  3. The string contains only a trailing comma
  4. The string contains no leading or trailing commas

We passed the following arguments to the String.slice method:

  1. start index - the index of the first character to 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 function is definitely a bit more verbose than the regex, but you might find it easier to read if you're not a fan of using regular expressions.

Further Reading #

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.