javascript - Regular expression /(\d)(?=(\d{3})+\.)/g
伊谢尔伦
伊谢尔伦 2017-05-19 10:35:23
0
1
671

How to add a comma to every three digits of the number to the left of a floating point number, such as converting 12000000.11 into "12,000,000.11"?

function commafy(num){

  return num && num
      .toString()
      .replace(/(\d)(?=(\d{3})+\.)/g, function(, ){
          return  + ',';
      });

}
I don’t know how to understand this regular rule. /(d)(?=(d{3}) .)/g
Not sure how it works

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
阿神

Match/(d)(?=(d{3})+.)/gThe number is followed by three digits or a multiple of 3, followed by a decimal point. d{3})+ means a number that is a multiple of 3, such as 3 numbers, 6 numbers, etc., ?= means that the following number must be a multiple of 3

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!