Nodejs String Trim trim()

Here you can find the source of trim()

Method Source Code

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function () {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function () {
    return this.replace(/\s+$/, "");
}

String.prototype.toStringWithCommas = function () {
    var parts = this.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");

};

// format function borrowed from http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format/4673436#4673436
String.prototype.format = function () {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function (match, number) {
        return typeof args[number] != 'undefined'
          ? args[number]//from  w ww  .  j  av  a2  s  .  c  om
          : match
        ;
    });
};

Related

  1. trim()
    String.prototype.trim = function () {
      return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
    };
    
  2. trim()
    String.prototype.trim = String.prototype.trim || function()
      return (this || '').replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    };
    String.prototype.seconds = String.prototype.seconds || function()
      return parseFloat(this) * 1000;
    String.prototype.attempts = String.prototype.attempts || function()
    ...
    
  3. trim()
    String.prototype.trim = function() {
      return this.replace(new RegExp('\n| ','g'), '');
    
  4. trim()
    String.prototype.trim = function () {
        return this.replace(/^\s*/, "").replace(/\s*$/, "");
    };
    
  5. trim()
    String.prototype.trim = function(){
      return this.replace(/^\s+|\s+$/g, '')
    String.prototype.ltrim = function(){
      return this.replace(/^\s+/,'')
    String.prototype.rtrim = function(){
      return this.replace(/\s+$/,'')
    String.prototype.fullTrim = function(){
      return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ')
    String.prototype.byteLength = function(){
      var count = 0;
      for(var i=0;i<this.length;i++)
        if(this.charCodeAt(i) >= 00000000 && this.charCodeAt(i) <= 0x0000007F)
          count++
        else if(this.charCodeAt(i) >= 0x00000080 && this.charCodeAt(i) <= 0x000007FF)
          count += 2
        else if(this.charCodeAt(i) >= 0x00000800 && this.charCodeAt(i) <= 0x0000FFFF)
          count += 3
        else if(this.charCodeAt(i) >= 0x00010000 && this.charCodeAt(i) <= 0x001FFFFF)
          count += 4
      return count
    
  6. trim()
    String.prototype.trim = function(){
      var t = this.replace(/(^\s*)|(\s*$)/g,""); 
      return t.replace(/(^\u3000*)|(\u3000*$)/g,"");
    };
    function killErrors(){
      return true;
    Date.prototype.format = function(format)
     var o = {
     "M+" : this.getMonth()+1, 
     "d+" : this.getDate(),    
     "h+" : this.getHours(),   
     "m+" : this.getMinutes(), 
     "s+" : this.getSeconds(), 
     "q+" : Math.floor((this.getMonth()+3)/3),  
     "S" : this.getMilliseconds() 
     if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
     (this.getFullYear()+"").substr(4 - RegExp.$1.length));
     for(var k in o)if(new RegExp("("+ k +")").test(format))
     format = format.replace(RegExp.$1,
     RegExp.$1.length==1 ? o[k] :
     ("00"+ o[k]).substr((""+ o[k]).length));
     return format;
    
  7. trim()
    String.prototype.trim = function() {
        try {
            return this.replace(/^\s+|\s+$/g, "");
        } catch(e) {
            return this;
    };
    
  8. trim()
    String.prototype.trim = function() {
        return this.replace(/^[ ]+|[ ]+$/g, '');
    };
    
  9. trim()
    String.prototype.trim = function() {
      return this.replace(/^\s+|\s+$/g, "");
    };