Javascript String commafy()

Description

Javascript String commafy()


// "1234567".commafy() returns "1,234,567"
String.prototype.commafy = function() {
  return this.replace(/(.)(?=(.{3})+$)/g,"$1,")
}

Javascript String commafy()

String.prototype.commafy = function(){
    return this.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

Javascript String commafy()

String.prototype.commafy = function () {
  return this.replace(/(^|[^\w.])(\d{4,})/g, function($0, $1, $2) {
    return $1 + $2.replace(/\d(?=(?:\d\d\d)+(?!\d))/g, "$&,");
  });/*from ww w . ja v  a 2 s.co m*/
}



PreviousNext

Related