JS uses regular expressions to add a thousand character to the integer part of a number for every three digits.

Today, I took a front-end written test. The last question is to give a long number. I want you to write a function to return the effect of adding a thousand character to the number

.

eg: input 1234567891.123, and finally output 1234567891.123, ask you how to achieve it.

I came back to check the data and found that it could be done with the zero-width assertion of the regular expression. It was written, but there were two questions that needed to be answered by the boss. I wrote them in the code .

var thousandBitSeparator = function(numStr){
     var b = /(?<=\d)(\d{3})/g;     //1.exp
     return numStr.replace(b, function($1){
          return ","+$1;
     });
}
alert(thousandBitSeparator("1234567891.123"));//1,234,567,891.123 
alert(thousandBitSeparator("12345678912.123"));//1,234,567,8912.123 2.

https://github.com/anran758/F.

try this..

< hr >

reminded by the friends in the comment area.. Fault tolerance is carried out for the decimal point, and the regular only takes the integer part.

  http://regex.zjmainstay.cn/r/.

Menu