1 .
function trim(text){
text=text.replace(/^/s+/, "").replace(//s+$/, "");
}
2.
function trim(text){
text=text.replace(/^/s+/, "");
for(var i=text.length-1;i>=0;i--){
if(//S/.test(text.charAt(i))){
text=text.substring(0,i+1);
break;
}
}
return text;
}
第一个适用于字符串较小的,第二个适用于较大的字符串,第二个比第一个性能更好速度更快