A recent project has used thousands to separate this feature, and some examples have been seen on the web, but it is a bit more complex to achieve. Therefore, I realized a thousands of separate, left to us later.
First on the source bar.
The method supports passing in a numeric string, a number. The second parameter is the number of digits reserved for decimals, and two decimal places are reserved by default;
function splitThousands(num, fixed) { var reg = /\B(?=(\d{3})+$)/g; num = num.toString().split("."); num[1] = num[1] ? num[1].substr(0, fixed || 2) : "00000000000000000".substr(0, fixed || 2); return num.join(".");}
The implementation of this method is very simple, it is a regular problem. In this implementation method, the difficulty is still this regular.
Only the burrow to learn the book, simply look at this regular:
\b with \d, \w and so on, take the opposite direction of \b. \b The word delimiter, it is clear that \b is a non-word delimiter. eg
var str = "hello world, hello MobroZhu, not Mobro Zhu";var reg = [/Mobro\b/g, /Mobro/g];console.log(reg[0], reg[0].exec(str) , "\n" + reg[1], reg[1].exec(str));
Print the log as follows:
/Mobro\b/g ["Mobro", index: 33, input: "hello world, hello MobroZhu, not Mobro Zhu"] "/Mobro/g" ["Mobro", index: 19, input: "hello world, hello MobroZhu, not Mobro Zhu"]
It is obvious that the Mobro found two times is not the same. Without \b, it means that as long as the match to Mobro is counted. So,/mobro/g will match to two Mobro, and/mobro\b/g will only match to the back one Mobro.
He thought, his heart, like this collapsed hole, dim and stuffy, no one is fit to live in;
And his feelings, in people's eyes, just like the mouse, but in the dark and sewage, grow battened, fierce can eat a cat, but still see no light, raves.
Each of the vulgar read Pro "Every jump, see the guy in the shop life cushion"
JS for thousands of separators