Nodejs String Trim trim( ext )

Here you can find the source of trim( ext )

Method Source Code

String.prototype.trim = function( ext )
{
   var chars = [/*from w  w  w .  j av a 2 s. c om*/
      " ", "\t", "\n", "\r"
   ];
   
   var s = this.copy();
   
   if( arguments.length > 0 ){
      for( var i in ext ){
         chars.push( ext[i] );
      }
   }
   
   while( chars.indexOf( s.charAt( 0 ) ) != -1 ){
      s = s.substring( 1, s.length );
   }
   while( chars.indexOf( s.charAt( s.length-1 ) ) != -1 ){
      s = s.substring( 0, s.length-1 );
   }
   return s;
};

Related

  1. trim || (String.prototype.trim()
    String.prototype.trim || (String.prototype.trim = function() {
        var str = this.replace(/^\s\s*/, ''),
            ws = /\s/,
            i = str.length;
        while(ws.test(str.charAt(--i)));
        return str.slice(0, i + 1);
    });
    
  2. trim()
    String.prototype.trim = (function () {
        var trimLeft  = /^\s+/,
            trimRight = /\s+$/;
        return function () {
          return this.replace(trimLeft, "").replace(trimRight, "");
    };
    
  3. trim( )
    String.prototype.trim = function( )
      return( this.replace(new RegExp("^([\\s]+)|([\\s]+)$", "gm"), "") );
    };
    
  4. trim(()
    String.prototype.trim = (function() {
      var trimRegex = /(^\s+|\s+$)/g;
      return function() {
        return this.replace(trimRegex, '');
      };
    }());
    
  5. trim(()
    String.prototype.trim = (function() {
            var trimRegex = /(^\s+|\s+$)/g;
            return function() {
                    return this.replace(trimRegex, '');
            };
    
  6. trim()
    String.prototype.trim = function(){
        return this.replace(/(^[\\s]*)|([\\s]*$)/g, "");
    
  7. trim()
    String.prototype.trim = function trim() {
      return this.replace(/^\s+|\s+$/g, '');
    };