function reFormat(num){ return ('' + num).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1 ') } alert(reFormat(10000)); alert(reFormat(1000000));