Nodejs String Format format()

Here you can find the source of format()

Method Source Code

/**//  w w  w.  j a  v a  2  s.c  o  m
 * Helper function to use string format, e.g., as known from C#
 * var awesomeWorld = "Hello {0}! You are {1}.".format("World", "awesome");
 *
 * TODO Enclose the format prototype function in HuddleClient JavaScript API.
 * Source: http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery
 */
String.prototype.format = function () {
    var args = arguments;
    return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) {
        if (m == "{{") { return "{"; }
        if (m == "}}") { return "}"; }
        return args[n];
    });
};

Related

  1. format()
    String.prototype.format = function() 
      var result = this;
      for (var ix = 0; ix < arguments.length; ix++) 
        var tagRegExp = new RegExp("\\{" + ix + "\\}", "gi");
        result = result.replace(tagRegExp, arguments[ix]);
      return result;
    ...
    
  2. 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;
    };
    function isNullOrWhitespace(input) {
    ...
    
  3. format()
    String.prototype.format= function(){
           var args = arguments;
           return this.replace(/\{(\d+)\}/g,function(s,i){
             return args[i];
           });
    
  4. format()
    function log(s){ 
      if(console)
        console.log(s);
    function callStack(){
      log(new Error().stack);
    String.prototype.format = function() {
      var args = arguments;
    ...
    
  5. format()
    'use strict'
    String.format = String.f = function () {
      var s = arguments[0]
      for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{' + i + '\\}', 'gm')
        s = s.replace(regexp, (arguments[i + 1] === null ? '' : arguments[i + 1]))
      return s
    String.prototype.format = String.prototype.f = function () {
      var s = this
      for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{' + i + '\\}', 'gm')
        s = s.replace(regexp, (arguments[i] === null ? '' : arguments[i]))
      return s
    
  6. format()
    function package(nspace)
        var nspaceComponents = nspace.split(".");
        var parent = window;
        for (var i = 0; i < nspaceComponents.length; ++i) {
            if (typeof parent[nspaceComponents[i]] === "undefined") {
                parent[nspaceComponents[i]] = {};
            parent = parent[nspaceComponents[i]];
    ...
    
  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()
    'use strict';
    String.prototype.format = function() {
      var content = this;
      for (var i=0; i<arguments.length; i++) {
        var placeholder = '{' + i + '}';
        content = content.replace(placeholder, arguments[i]);
      return content;
    };
    ...
    
  9. format()
    var width = 584
    var height = 329
    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;
    ...