1. 去掉字串兩端的空格
String.prototype.trim=function (){return this.replace(/(^/s*)|(/s*$)/g,'');}
2. 去掉字串中所有的空格:
String.prototype.sTrim = function (){return this.replace(//s/g, ''); }
或:
var s = "asd ddd bbb sss";
var reg = //s/g;
var ss = s.replace(reg, "");
alert(ss);