Nodejs String Trim trim(c)

Here you can find the source of trim(c)

Method Source Code

/**//from www  .j  av  a2s.  c o m
 * Trim all whitespaces OR a character (if specified) from the begining and the end of a string
 * @param [Object String] c Character to be removed (optional)
 * @return [Object String] Trimmed string
 */
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,'');
    }
};

/**
 * Uppercase the first character of a string
 * @return [Object String] Capitalized string
 */
String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
};

/**
 * Add the string to itself n times
 * @param {number} n
 * @return {string} New String
 */
String.prototype.times = function(n) { return (new Array(n+1)).join(this);};

Related

  1. trim()
    String.prototype.trim = function() {
      return this.replace(/^\s+/, "").replace(/\s+$/, "");
    };
    
  2. trim()
    String.prototype.trim = function() {
        "use strict";
        return this.replace(/^\s+|\s+$/g,"");
    };
    
  3. trim()
    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;
    ...
    
  4. 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);
    ...
    
  5. trim()
    String.prototype.trim = function() {
        return this.replace(/^\s+/g,"").replace(/\s+$/g,"");
    };
    
  6. 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"), "");
    };
    
  7. 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"), "");
    };
    
  8. trim(ch)
    String.prototype.trim = function(ch) {
        var r = new RegExp("^{0}+|{0}+$".format(ch || "\\s"), "g");
        return this.replace(r, "");
    };
    
  9. trim(character)
    String.prototype.trim = function (character) {
      return this.trimLeft(character).trimRight(character);
    };