function capacity(number) { return String(number).replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ') } alert( capacity(2352) ); alert( capacity(5643547) ); alert( capacity(53264324) );