var text = "text 1234567890 text"; var re = /(?=\B(?:\d{3})+(?!\d))/g; alert( text.replace( re, ' ' ) );