Nodejs String Trim trim()

Here you can find the source of trim()

Method Source Code

/**//from w  w  w  .  ja  v a 2s . com
   @name String
   @class Additions to the core string object.
*/

/** @author Steven Levithan, released as public domain. */
String.prototype.trim = function() {
   var str = this.replace(/^\s+/, '');
   for (var i = str.length - 1; i >= 0; i--) {
      if (/\S/.test(str.charAt(i))) {
         str = str.substring(0, i + 1);
         break;
      }
   }
   return str;
}
/*t:
   plan(6, "Testing String.prototype.trim.");
   
   var s = "   a bc   ".trim();
   is(s, "a bc", "multiple spaces front and back are trimmed.");

   s = "a bc\n\n".trim();
   is(s, "a bc", "newlines only in back are trimmed.");
   
   s = "\ta bc".trim();
   is(s, "a bc", "tabs only in front are trimmed.");
   
   s = "\n \t".trim();
   is(s, "", "an all-space string is trimmed to empty.");
   
   s = "a b\nc".trim();
   is(s, "a b\nc", "a string with no spaces in front or back is trimmed to itself.");
   
   s = "".trim();
   is(s, "", "an empty string is trimmed to empty.");

*/

Related

  1. trim()
    String.prototype.trim = function () {
        var start, end;
        start = 0;
        end = this.length - 1;
        while (start <= end && str.charAt(start) == ' ') {
            start++;
        while (start <= end && str.charAt(end) == ' ') {
            end--;
    ...
    
  2. trim()
    exports.version = '0.0.1';
    String.prototype.trim = function() { 
      return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    
  3. trim()
    String.prototype.trim = function () {
      return this.replace(/^ +/, '').replace(/ +$/, '');
    };
    
  4. trim()
    String.prototype.trim = function() {
      return this.replace(/^\s+/, "").replace(/\s+$/, "");
    };
    
  5. trim()
    String.prototype.trim = function() {
        "use strict";
        return this.replace(/^\s+|\s+$/g,"");
    };
    
  6. trim()
    String.prototype.trim = function(){
      var whitespace  = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
      var l      = 0;
      var i      = 0;
      var str      = this + '';
      l = str.length;
      for(i = 0; i < l; i++){
        if(whitespace.indexOf(str.charAt(i)) === -1){
          str = str.substring(i);
    ...
    
  7. trim()
    String.prototype.trim = function() {
        return this.replace(/^\s+/g,"").replace(/\s+$/g,"");
    };
    
  8. trim(c)
    String.prototype.trim = function (c) {
        if (c) {
            var r = new RegExp('(^' + c + ')|(' + c + '$)', 'g');
            return this.replace(r, '');
        } else {
            return this.replace(/^\s+|\s+$/g,'');
    };
    String.prototype.capitalize = function() {
    ...
    
  9. trim(c, t)
    String.prototype.trim = function(c, t){
      return c = "[" + (c == undefined ? " " : c.replace(/([\^\]\\-])/g, "\\\$1")) + "]+",
      this.replace(new RegExp((t != 2 ? "^" : "") + c + (t != 1 ? "|" + c + "$" : ""), "g"), "");
    };