String.prototype.Trim = function() {
   return this.replace(/(^\s*)|(\s*$)/g, "");
    }
alert("s"+"   aa   ".Trim()+"w");

解决方案 »

  1.   

    或者写成
    function f(str){
       return str.replace(/(^\s*)|(\s*$)/g, "");
     }
      

  2.   

    String.prototype.trim=function(){
    return this.replace(/^\s*/g,"").replace(/\s*$/g,"");
    }
    //去掉首尾空格的
    //用法  //"   fdsaf   ".trim()=="fdsaf"
    String.prototype.lTrim=function(){
    return this.replace(/^\s*/g,"");
    }
    //去掉左边起头的空格
    //"       d".lTrim()=="d"String.prototype.rTrim=function(){
    return this.replace(/\s*$/g,"");
    }
    //去掉右边结尾的空格
    //"ffdsafdsa    ".rTrim()=="ffdsafdsa"