Nodejs String Trim trim()

Here you can find the source of trim()

Method Source Code

/** @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);/*w w  w.ja va 2s  .c o m*/
      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 reExtraSpace = /^\s+(.*?)\s+$/;
      return this.replace(reExtraSpace, "$1");
    };
    var script = document.getElementsByTagName("script");
    var template = {};
    for(var i = 0; i < script.length; i++){
      if(script[i].getAttribute("type") == "template"
        && script[i].id)
    ...
    
  2. 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);
    };
    
  3. trim()
    String.prototype.trim = function() {
      return this.replace(/^\s*/, "").replace(/\s*$/, "");
    };
    
  4. trim()
    String.prototype.trim=function()
      return this.replace(/(\s*$)|(^\s*)/g, "");
    
  5. trim()
    String.prototype.trim = function()
        return this.replace(/^\s+/g, '').replace(/\s+$/g, '');
    };
    
  6. trim()
    String.prototype.trim = function() {
        return this.replace(/(^\s+)|(\s+$)/g, "");
    
  7. trim()
    String.prototype.trim = function() {
      a = this.replace(/^\s+/, '');
      return a.replace(/\s+$/, '');
    };
    
  8. trim()
    String.prototype.trim = function () {
      "use strict";
      return this.replace(/^\s+|\s+$/g, '');
    };
    
  9. trim()
    String.prototype.trim = function() {
      var re = /^\s+|\s+$/g;
      return this.replace(re, "");