0
num.replace(/\B(?=(\d{2})+(?!\d))/g, ",")

The above regex is not working properly.

ex- if value= 123456789 result should be= 12,34,56,789

if value= 12345678 result should be= 1,23,45,678

if value= 123456 result should be= 1,23,456

Please suggest.

Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140

1 Answers1

1

You can create a extension method as follows

Number.prototype.format = function(){
   return this.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
};
var num=123456789;
alert(num.format());

Please see the demo here, it is working fine.

Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140