Nodejs String Format format(options)

Here you can find the source of format(options)

Method Source Code

String.prototype.format = String.prototype.f = function(options) {

    options = options ? options : [];//from  w ww.  j  a v  a2 s  .c o m
    if (typeof options != "object") options = [options];

    var s = this,
        i = options.length;

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

    return s;
 };

Related

  1. format()
    String.prototype.format = String.prototype.f = function () {
        var args = arguments;
        return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) {
            if (m == "{{") { return "{"; }
            if (m == "}}") { return "}"; }
            return args[n];
        });
    };
    
  2. format()
    String.prototype.format = String.prototype.f = function () {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };
    
  3. format()
    String.prototype.format = String.prototype.f = function() {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };
    
  4. format()
    String.prototype.format = String.prototype.f = function() {
      var s = this,
          i = arguments.length;
      while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
      return s;
    };
    
  5. format()
    'use strict';
    String.prototype.format = String.prototype.f = function() {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };
    ...
    
  6. format()
    String.prototype.format = String.prototype.format = function() {
      var s = this,
        i = arguments.length;
      while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
      return s;
    };
    
  7. format()
    String.prototype.format = String.prototype.format ||
    function () {
        var str = this.toString();
        if (arguments.length) {
            var t = typeof arguments[0];
            var key;
            var args = ("string" === t || "number" === t) ?
                Array.prototype.slice.call(arguments)
                : arguments[0];
    ...
    
  8. format()
    String.prototype.format = String.prototype.format || function () {
      var string = this
      for (var i = 0, j = arguments.length; i < j; i++) {
        string = string.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i])
      return string
    
  9. format()
    String.prototype.format = String.prototype.format || function format() {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function($0, $1) {
            return args[+$1];
        });
    };