Nodejs String Format format()

Here you can find the source of format()

Method Source Code

String.prototype.format = function()
{
    var args = arguments;
    return this.replace(/\{(\d+)\}/g,                
        function(m,i){
            return args[i];
        });// w  w  w  .j ava2s  .  c o  m
}

Related

  1. format()
    String.prototype.format = function() {
      a = this;
      for (k in arguments) {
        a = a.replace("{" + k + "}", arguments[k]);
      return a;
    };
    
  2. format()
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/{(\d{1})}/g, function() {
        return new String(args[arguments[1]]);
      });
    };
    
  3. format()
    String.prototype.format=function(){
      if(0==arguments.length)
         return this;
      var t=arguments;
      return this.replace(/\{(\d+)\}/g,function(r,n){
         return void 0===t[n]?"{"+n+"}":t[n]})
    };
    
  4. format()
    String.prototype.format = function() {
        var newStr = this, i = 0;
        while (/%s/.test(newStr))
            newStr = newStr.replace("%s", arguments[i++])
        return newStr;
    
  5. format()
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function() {
            return args[arguments[1]];
        });
    };
    
  6. format()
    String.prototype.format = function(){
        var args = arguments;
        return this.replace(/\{(\d)\}/g, function(a,b){
            return typeof args[b] != 'undefined' ? args[b] : a;
        });
    
  7. format()
    var util = require('util');
    String.prototype.format = function () {
        return util.format.apply(null,
                [ this.toString() ].concat(Array.prototype.slice.call(arguments)));
    };
    
  8. format()
    String.prototype.format = function(){
        var pattern = /\{\d+\}/g;
        var args = arguments;
        return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; });
    
  9. format()
    String.prototype.format = function() {
      var formatted = this;
      _.forEach(arguments, function(arg, i) {
        formatted = formatted.replace("{" + i + "}", arg);
      });
      return formatted;
    };