当前位置:编程学习 > JS >>

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), "");

   }

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,