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.