.split(/(\d{3})/).join(' ').replace(/\s+/g, ' ').replace(/^\s+/, '').replace(/\s+$/, ''));
var n = 12345678;
n = n.toString(10).replace(/(\d)(?=(\d{3})+$)/g, '$1 '); // 12 345 678
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"