js函數擴展

 

擴展去除字符串後面的字符或者空格

 

 

 

 

//去掉字符串兩端的字符

String.prototype.trim = function() {

       var charlist = arguments[0] || ''; if(charlist == '') return this.replace(/(^\s*)|(\s*$)/g, "");

       else   return this.replace(eval('/(^\\'+charlist+'*)|(\\'+charlist+'*$)/g'), "");

       }

 

 

 

/*** 默認去掉字符串左邊所有空白

*/

String.prototype.ltrim = function(charlist) {

    var charlist = charlist || '';

   if(charlist == ''){

      return this.replace(/(^\s*)/g, "");

   }else{

      var reg = '/(^\\'+charlist+'*)/g';

      return this.replace(eval(reg), "");

   }

}

/*** 默認去掉字符串右邊所有空白*/

String.prototype.rtrim = function(charlist) {

    var charlist = charlist || '';

   if(charlist == ''){

      return this.replace(/(\s*$)/g, "");

   }else{

      var reg = '/(\\'+charlist+'*$)/g';

      return this.replace(eval(reg), "");

   }

You May Also Like