0

I used the following regex

var x=32423332.343;
var res= x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

which gives an output of 32,423,332.343

How do I modify this regex (shortest way) to get the following output

3,24,23,332.343

1 Answers1

1

Well, if you want that, you can modify your regex a bit:

\B(?=(?:\d{2})*\d{3}(?!\d))

regex101 demo

(?:\d{2})* will match even number of digits before the final \d{3}.

For PCRE engine, one that can handle integers and floating, with g enabled.

\G\d{1,2}\K\B(?=(?:\d{2})*\d{3}(?!\d))
Jerry
  • 70,495
  • 13
  • 100
  • 144