十进制数的正则表达式[重复]

时间:2022-05-07 16:15:07

This question already has an answer here:

这个问题在这里已有答案:

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

它的输出为32,423,332.343

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

如何修改此正则表达式(最短路径)以获得以下输出

3,24,23,332.343

1 个解决方案

#1


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}.

(?:\ d {2})*将匹配最终\ d {3}之前的偶数位数。

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

对于PCRE引擎,可以处理整数和浮动,启用g。

\G\d{1,2}\K\B(?=(?:\d{2})*\d{3}(?!\d))

#1


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}.

(?:\ d {2})*将匹配最终\ d {3}之前的偶数位数。

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

对于PCRE引擎,可以处理整数和浮动,启用g。

\G\d{1,2}\K\B(?=(?:\d{2})*\d{3}(?!\d))