var str = "word word 1234567 more words 12345678.12345 and 123456 and 1234567890.1234567 some word"; alert(str.replace(/(\d+)(\.\d+)?/g, function (c, b, a) { return b.replace(/(\d)(?=(\d{3})+$)/g, "$1 ") + (a ? a : "") }))