var a = "tyty 13788656456456456 tyty"; alert(a.replace(/(\d{1,3})(?=((\d{3})*([^\d]|$)))/g, " $1 "));