Nodejs String Format format()

Here you can find the source of format()

Method Source Code

function $log(text, obj) {
  if (window.console && console.log) {
    console.log(text);//from  w  w w.j av  a 2 s  .  co m
    if (obj) {
      console.log(obj);
    }
  }
}

String.prototype.format = function () {
  var s = this,
      i = arguments.length;

  while (i--) {
    s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
  }
  return s;
};

Related

  1. format()
    'use strict';
    String.prototype.format = function(){
        var args = arguments;
        return this.replace(/{(\d+)}/g, function(match, index){
            return args[index];
        });
    };
    module.exports = String;
    
  2. format()
    String.prototype.format = function() {
        var formatted = this;
        for (var arg in arguments) {
            formatted = formatted.replace("{" + arg + "}", arguments[arg]);
        return formatted;
    };
    $(document).on("click", "input.click-select", function(e) {
        $(e.target).select();
    ...
    
  3. format()
    "use strict";
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function () {
            return args[arguments[1]];
        });
    };
    
  4. format()
    String.prototype.format = function() {
      var pattern = /\{\d+\}/g;
      var args = arguments;
      return this.replace(pattern, function(capture){return args[capture.match(/\d+/)]});
    
  5. format()
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != 'undefined'
                ? args[number]
                : match
                ;
        });
    };
    ...
    
  6. format()
    String.prototype.format = function() {
      var args = [].slice.call(arguments);
      return this.replace(/%(\d+)/g, function(w, m) {
        var offset = parseInt(m) - 1;
        if (args[offset] != undefined) {
          return String(args[offset]);
        return w;
      });
    ...
    
  7. format()
    String.prototype.format=function()  
        if(arguments.length==0) return this;  
        for(var s=this, i=0; i<arguments.length; i++)  
          s=s.replace(new RegExp("\\{"+i+"\\}","g"), arguments[i]);  
        return s;  
      };
    
  8. format()
    String.prototype.format = function() {
        var str = this.toString();
        if (!arguments.length)
            return str;
        var args = typeof arguments[0],
            args = (("string" == args || "number" == args) ? arguments : arguments[0]);
        for (arg in args)
            str = str.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]);
        return str;
    ...
    
  9. format()
    String.prototype.format = function () {
        var result = this.toString();
        var arg = arguments.length == 1 && typeof arguments[0] == 'object' ? arguments[0] : arguments;
        for (var value in arg) {
            result = result.split('{' + value + '}').join(arg[value]);
        return result;