cut the unwanted tail  [ 689 views ]

There is a big array and the items joined by comma like this:

var arr = [...];
var joined = arr.join(',');
console.log(joined);

-> a,w,e,,,,q,,t,z,,,,,,,,,,,

I don’t need the end with the many unwanted comma. I try to cut the string at the last real value.
Let’s extend the string object with the followings:

if (!String.prototype.dropFromEnd) {
  Object.defineProperty(String.prototype, 'dropFromEnd', {
    enumerable: false,
    configurable: false,
    writable: false,
    value: function (searchString) {
      var regEx = new RegExp(searchString + '+$', 'g');
      return this.replace(regEx,"");
    }
  });
};

to call this:

var joined = 'a,w,e,,,,q,,t,z,,,,,,,,,,,'.dropFromEnd(',');
console.log(joined);

-> a,w,e,,,,q,,t,z

#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }