Nodejs String Format format()

Here you can find the source of format()

Method Source Code

/**//www  .  j a v a  2  s  .c  om
 * Format function that replace placeholders in strings with values
 * Usage : "This is {0} formatted {1}".format("a", "string");
 **/
String.prototype.format = function() {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{'+i+'\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[i]);
    }
    return formatted;
};

Related

  1. 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;
    };
    
  2. format()
    String.prototype.format = function () {
        var formatted = this;
        for (var arg in arguments) {
            formatted = formatted.replace("{" + arg + "}", arguments[arg]);
        return formatted;
    };
    
  3. format()
    String.prototype.format = function () {
        var formatted = this;
        for (var i = 0; i < arguments.length; i++) {
            var regexp = new RegExp('\\{' + i + '\\}', 'gi');
            formatted = formatted.replace(regexp, arguments[i]);
        return formatted;
    };
    String.prototype.replaceAll = function (find, replace) {
    ...
    
  4. format()
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g, function() {
            return args[arguments[1]];
        });
    };
    
  5. format()
    String.prototype.format = function () {
      var formatted = this;
      for (var prop in arguments[0]) {
        var regexp = new RegExp('\\{' + prop + '\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[0][prop]);
      return formatted;
    };
    if (typeof String.prototype.trim !== 'function') {
    ...
    
  6. format()
    String.prototype.format = function() {
        var txt = this, i = arguments.length; while (i--) {
            txt = txt.replace (
                new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]
            );
        return txt;
    };
    
  7. format()
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/\{(\d+)\}/g, function(match, number) {
        return typeof args[number] != 'undefined' ? args[number] : match;
      });
    };
    
  8. format()
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != 'undefined' ? args[number] : match;
        });
    };
    
  9. format()
    String.prototype.format = function () {
        var str = this;
        for (var i = 0; i < arguments.length; i++) {
            var reg = new RegExp("\\{" + i + "\\}", "gm");
            str = str.replace(reg, arguments[i]);
        return str;