var str ="Rome was not built in a day"; arr =str.replace(/\s/g);//去掉所有的空格 var wd =0; for(var i of str) { wd++; } document.write(wd);