var str = 'Lorem ipsum 234456234 Lorem ipsum 7345287346 asdfae'; alert(str.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 '));