Nodejs String Format format()

Here you can find the source of format()

Method Source Code

String.prototype.format = function() {
  var formatted = this;
  _.forEach(arguments, function(arg, i) {
    formatted = formatted.replace("{" + i + "}", arg);
  });//  w w w .j a v  a 2 s.  com
  return formatted;
};

Related

  1. format()
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function() {
            return args[arguments[1]];
        });
    };
    
  2. format()
    String.prototype.format = function()
        var args = arguments;
        return this.replace(/\{(\d+)\}/g,                
            function(m,i){
                return args[i];
            });
    
  3. format()
    String.prototype.format = function(){
        var args = arguments;
        return this.replace(/\{(\d)\}/g, function(a,b){
            return typeof args[b] != 'undefined' ? args[b] : a;
        });
    
  4. format()
    var util = require('util');
    String.prototype.format = function () {
        return util.format.apply(null,
                [ this.toString() ].concat(Array.prototype.slice.call(arguments)));
    };
    
  5. format()
    String.prototype.format = function(){
        var pattern = /\{\d+\}/g;
        var args = arguments;
        return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; });
    
  6. format()
    'use strict';
    String.prototype.format = function() {
      var formatted = this;
      for (var arg in arguments) {
        formatted = formatted.replace('{' + arg + '}', arguments[arg]);
      return formatted;
    };
    
  7. format()
    String.prototype.format = function() {
        var s = this,
            i = arguments.length
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i])
        return s
    
  8. format()
    String.prototype.format = function() {
        var formatted = this;
        for( var arg in arguments ) {
            formatted = formatted.replace("{" + arg + "}", arguments[arg]);
        return formatted;
    };
    
  9. format()
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/{(\d+)}/g, function(match, number) { 
        return typeof args[number] != 'undefined'
        ? args[number]
        : match
        ;
      });
    };
    ...