var str = '6222023100014763381'; var str=str.replace(/\s/g,'').replace(/(.{4})/g,"$1 "); console.log(str);