How to make the input spaces from the end of my regular season?

.split(/(\d{3})/).join(' ').replace(/\s+/g, ' ').replace(/^\s+/, ").replace(/\s+$/, "));

In this way every 3 digits to put in the blanks, but they come at the end, that is, for 5000 get 500 0. or 532 123 2 instead of 5 321 232. How to do it right?
June 27th 19 at 15:35
3 answers
June 27th 19 at 15:37
Solution
In the end only drove this decision for my curve case artkiev.com/blog/number_format-in-javascript.htm
June 27th 19 at 15:39
The regular season:
var n = 12345678;
n = n.toString(10).replace(/(\d)(?=(\d{3})+$)/g, '$1 '); // 12 345 678


It is possible and without regexps. String split into an array, to deploy, to every third character, to attach the gap (go from the end, it turns out), gather in the line:

var n = 123456789;

var a = n
.toString(10)
.split(").reverse()
 .map(function(e,i){ return (i%3 ? e : ""+e+" ")})
.reverse()
.join(")
.trim()
;
// a = "123 456 789"


Disclaimer: incorrect can handle the minus sign!
Probably somewhere in other place I cant. There is a complex logic and a lot of events, but in the end my option 1 actually works, the rest put after 3 every 1 space.
I have rangeSlider, events in focus\Blair, modifications to strings and back, the entry in the hidden ...... - amelia_Schroed commented on June 27th 19 at 15:42
so still 2 ways you know
stackoverflow.com/questions/3753483/javascript-tho...
.replace(/\B(?=(\d{3})+(?!\d))/g, " ") - amelia_Schroed commented on June 27th 19 at 15:45
June 27th 19 at 15:41
That's still possible:
var str = "12345678";
var res = "";

for (var i = str.length; i >= 0; i -= 3) {
 var sub = i > 3 ? str.substr(i - 3, 3) : str.substr(0, i);
 res = sub + "" + res;
}

res = res.slice(0, res.length -1);
// 12 345 678

Find more questions by tags Regular expressionsJavaScript