js 仿phptrim, js trim
function trims(){ this.init = function(myarguments){ if(arguments.length===0){return false;} this.arg = myarguments; this.len = this.arg.length; if(this.len>0){ this.str = String(this.arg[0]); } if(this.len>1){ this.thechar = this.arg[1]; } if(typeof this.thechar=='undefined'){ this.rg_l = new RegExp("^(\\s|\\u00A0)+"); this.rg_r = new RegExp("\\S"); }else{ this.rg_l = new RegExp("^("+this.thechar+")+"); this.rg_r = new RegExp("[^"+this.thechar+"]{1}"); } }; if (typeof trims._initialized == "undefined") { trims.prototype.ltrim = function() { this.str = this.str.replace(this.rg_l,''); }; trims.prototype.rtrim = function() { for(var i=this.str.length-1; i>=0; i--){ if(this.rg_r.test(this.str.charAt(i))){ this.str = this.str.substring(0, i+1); break; } } if(i===-1){this.str = '';} }; trims._initialized = true; } }; var trimsobj = new trims(); function trim(){ trimsobj.init(arguments); trimsobj.ltrim(); trimsobj.rtrim(); return trimsobj.str; } function rtrim(){ trimsobj.init(arguments); trimsobj.rtrim(); return trimsobj.str; } function ltrim(){ trimsobj.init(arguments); trimsobj.ltrim(); return trimsobj.str; }