function replacer(str, p1, p2, offset, s) { return p1 + " "; } newString = "1234567890021412414144".replace(/^0*/, '').replace(/(\d)(?=(\d{3})+(?!\d))/g, replacer) alert(newString);