Nodejs String Trim trim(ws)

Here you can find the source of trim(ws)

Method Source Code

/*// w  w  w  . java 2 s  .  com
  stripNL()           - Alle Newlines entfernen
  trim([true/false])  - alle Leerzeichen am Anfang und Ende entfernen m. Parame doppelte einfach machen
  ltrim()             - Leerzeichen Links entfernen
  rtrim()             - Leerzeichen Rechts entfernen

*/
String.prototype.trim = function (ws)
{
    if(!this.length) return "";
    var tmp = this.stripNL().ltrim().rtrim();
    if(ws) return tmp.replace(/ +/g, ' ');
    else return tmp;
}
String.prototype.rtrim = function ()
{
    if(!this.length) return "";
    return this.replace(/\s+$/g, '');
}

String.prototype.ltrim = function ()
{
    if(!this.length) return "";
    return this.replace(/^\s+/g, '');
}
String.prototype.stripNL = function ()
{
    if(!this.length) return "";
    return this.replace(/[\n\r]/g, '');
}

Related

  1. trim(charlist)
    'use strict';
    String.prototype.trim = function (charlist) {
      return this.ltrim(charlist).rtrim(charlist);
    };
    
  2. trim(chars)
    String.prototype.trim = function(chars) {
      return this.replace(new RegExp('^[' + (chars || '\\s') + ']+|[' + (chars || '\\s') + ']+$', 'g'), '');
    };
    
  3. trim(fag='default')
    String.prototype.trim = function (fag='default'){
      const preg_arr = {'default':/(^\s*)|(\s*$)/,'left':/(^\s*)/,'right':/(\s*$)/};
      switch(fag){
        case 'left':
          return this.replace(preg_arr.left,'');
        break;
        case 'right':
          return this.replace(preg_arr.right,'');
        break;
    ...
    
  4. trim(str)
    String.prototype.trim = function(str) {
      str = str.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;
    ...
    
  5. trim(str)
    var trim = function (str) {
        return str.replace(/\s*/g, "");
    };
    
  6. trim2()
    String.prototype.trim2 = function() {
      return this.replace(/^\s+|\s+$/g, '');
    };
    String.prototype.trim = function() {
      return this.replace(/^\s+/, '').replace(/\s+$/, '');
    };
    'foo     '.trim(); 
    '     foo'.trim(); 
    '  foo   '.trim(); 
    ...
    
  7. trimBoth(s)
    String.prototype.trimBoth = function(s) {
        return this.replace(new RegExp("^" + s + "+|" + s + "+$", "gm"), "");
    
  8. trimChars(charlist)
    String.prototype.trimChars = function(charlist) {
      charlist = charlist || ' \r\n\t';
        var l = 0, i = 0;
        var ret = this;
        l = ret.length;
        for (i = 0; i < l; i++) {
            if (charlist.indexOf(ret.charAt(i)) === -1) {
                ret = ret.substring(i);
                break;
    ...
    
  9. trimSlash()
    String.prototype.trimSlash = function () {
        var string = this.trim();
        if (string.charAt(0) == "/") {
            string = string.substring(1);
        if (string.charAt(string.length - 1) == "/") {
            string = string.substring(0, string.length - 1);
        return string;
    ...