/* 刪除左右空格 */ function trim(string){return string.replace(/(^s*)|(s*$)/g,'');}; //函數法 String.prototype.__defineGetter__("trim",function(){return this.replace(/(^s*)|(s*$)/g,'')}) //prototype法 /* 刪除左側空格 */ function ltrim(string){return string.replace(/^s+/,'');}; //函數法 String.prototype.__defineGetter__("ltrim",function(){return this.replace(/^s+/,'')}) //prototype法 /* 刪除右側空格 */ function rtrim(string){return string.replace(/s+$/,'');}; //函數法 String.prototype.__defineGetter__("rtrim",function(){return this.replace(/s+$/,'')}) //prototype法 /* 刪除所有空格 */ function alltrim(string){return string.replace(/s/g,'');}; //函數法 String.prototype.__defineGetter__("alltrim",function(){return this.replace(/s/g,'')}) //prototype法 /* 刪除指定字符 */ function delChar(string,char){var charReg=new RegExp(char,'ig');return string.replace(charReg,'');} //prototype法使用說明 var a=" a b c d e "; console.log(a.ltrim); //輸出"a b c d e " console.log(a.rtrim); //輸出" a b c d e" console.log(a.trim); //輸出"a b c d e" console.log(a.alltrim); //輸出"abcde"