74

I have a string that is like below.

,liger, unicorn, snipe,

how can I trim the leading and trailing comma in javascript?

1

5 Answers 5

173

because I believe everything can be solved with regex:

var str = ",liger, unicorn, snipe,"
var trim = str.replace(/(^,)|(,$)/g, "")
// trim now equals 'liger, unicorn, snipe'
2
  • 1
    @eyelidlessness Is there are joke related to your comment? I'm OutOfTheLoop
    – mohitmun
    Commented Mar 31, 2017 at 7:52
  • 12
    For @mohitmun (and anyone else who comes along). 'Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.' More info: blog.codinghorror.com/…
    – knickum
    Commented Jun 27, 2017 at 14:48
42

While cobbal's answer is the "best", in my opinion, I want to add one note: Depending on the formatting of your string and purpose of stripping leading and trailing commas, you may also want to watch out for whitespace.

var str = ',liger, unicorn, snipe,';
var trim = str.replace(/(^\s*,)|(,\s*$)/g, '');

Of course, with this application, the value of using regex over basic string methods is more obvious.

2
  • 2
    the whitespace addition was the clincher for me.
    – Joe
    Commented Sep 9, 2013 at 12:52
  • if anybody's interested about trimming spaces also, better use herostwist's answer below, its better.
    – Sharky
    Commented Mar 27, 2017 at 13:38
37

If you want to make sure you don't have any trailing commas or whitespace, you might want to use this regex.

var str = ' , , , foo, bar,    ';
str = str.replace(/(^[,\s]+)|([,\s]+$)/g, '');

returns

"foo, bar"
7
  • 1
    For those wondering why this differs from eyelidlessness's, this solution strips spaces that are before the trailing comma or after the opening comma.
    – SamGoody
    Commented Feb 2, 2015 at 10:02
  • 1
    It also handles multiple leading or trailing commas. For example, ,,,liger, unicorn, snipe,,, -> liger, unicorn, snipe. Commented Mar 5, 2017 at 4:35
  • 2
    It doesn't replace the commas coming after snipe. This is better str.replace(/^[,\s]+|[,\s]+$/g, '').replace(/,[,\s]*,/g, ',');
    – cyonder
    Commented Apr 24, 2018 at 3:56
  • yes it does @cyonder - https://jsfiddle.net/hws45zpd/
    – herostwist
    Commented Apr 25, 2018 at 15:49
  • Well you have moved the goalposts @cyonder, the question was "how can I trim the leading and trailing comma". In order to make your comment valid you have had to invent new problems.
    – herostwist
    Commented Apr 28, 2018 at 16:43
21

Try this, since not everything can be solved by REs and even some things that can, shouldn't be :-)

<script type="text/javascript">
    var str = ",liger, unicorn, snipe,";
    if (str.substr(0,1) == ",") {
        str = str.substring(1);
    }
    var len = str.length;
    if (str.substr(len-1,1) == ",") {
        str = str.substring(0,len-1);
    }
    alert (str);
</script> 
1
  • 5
    Ask not what REs can do for you, but what you can do for REs! :)
    – cobbal
    Commented Mar 19, 2009 at 8:22
11

In ECMAScript 5 and above, you can now do a one-liner

',liger, unicorn, snipe,'.split(',').map(e => e.trim()).filter(e => e).join(', ')
1
  • perfect solution Commented Jul 22, 2022 at 6:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.