function ec() {
var ss = " fsd fsdaf ";
//去所有空格
alert(ss.replace(/[ ]/g, ""));
//去两边空格
alert("1" + ss.Trim() + "2");
//去左边空格
alert("1" + ss.LTrim() + "2");
//去右边空格
alert("1" + ss.RTrim() + "2");
}
String.prototype.Trim = function () { return this.replace(/(^\s*)|(\s*$)/g, ""); }
String.prototype.LTrim = function () { return this.replace(/(^\s*)/g, ""); }
String.prototype.RTrim = function () { return this.replace(/(\s*$)/g, ""); }
本文来自博客园,作者:董锡振,转载请注明原文链接:https://www.cnblogs.com/dongxizhen/archive/2012/10/09/js.html