Nodejs String Format format()

Here you can find the source of format()

Method Source Code

String.prototype.format = function() {
  var source = this;
  for (var i = 0; i <= arguments.length - 1; i++) {       
    var reg = new RegExp("\\{" + i + "\\}", "gm");             
    source = source.replace(reg, arguments[i]);
  }// www  .  j  a v  a  2 s . c  o m
  return source;
};

Date.prototype.format = function (fmt) { //author: meizz 
  var o = {
    "M+": this.getMonth() + 1, //?? 
    "D+": this.getDate(), //? 
    "h+": this.getHours(), //?? 
    "m+": this.getMinutes(), //? 
    "s+": this.getSeconds(), //? 
    "q+": Math.floor((this.getMonth() + 3) / 3), //?? 
    "S": this.getMilliseconds() //?? 
  };
  if (/(Y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
  if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
};

Related

  1. format()
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/\{(\d+)\}/g, function(orig,num) {
        return typeof args[num] != 'undefined' ? args[num] : orig;
      });
    };
    
  2. format()
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function(match, number) {
            return typeof args[number] != 'undefined' ? args[number] : match;
        });
    };
    
  3. format()
    String.prototype.format = function() {
      var placeholderREG = /\{(\d+)\}/g;
      var args = arguments;
      return this.replace(placeholderREG, function(holder, num) {
        return args[num];
      });
    
  4. format()
    String.prototype.format = function() {
      var formatted = this;
      for( var arg in arguments ) {
        formatted = formatted.replace("{" + arg + "}", arguments[arg]);
      return formatted;
    };
    function assert(condition, msg) {
      if (!condition) {
    ...
    
  5. format()
    'use strict';
    String.prototype.format = function () {
      var args = arguments;
      return this.replace(/\{(\d+)\}/g, function (m, n) {
        return args[n];
      });
    };
    String.prototype.escapeRegExp = function () {
      return this.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
    ...
    
  6. format()
    String.prototype.format = function() {
      var formatted = this;
      for (arg in arguments) {
        formatted = formatted.replace('{' + arg + '}', arguments[arg]);
      return formatted;
    };
    
  7. format()
    String.prototype.format = function() {
        var args = arguments;
        return this.replace(/\{(\d+)\}/g,
            function(m, i) {
                return args[i];
            });
    };
    String.prototype.encodeHTML = function () {
        var s = this.valueOf();
    ...
    
  8. format()
    String.format = function () {
        var s = arguments[0];
        for (var i = 0; i < arguments.length - 1; i++) {
            var reg = new RegExp("\\{" + i + "\\}", "gm");
            s = s.replace(reg, arguments[i + 1]);
        return s;
    String.prototype.format = function () {
    ...
    
  9. format()
    String.prototype.format = function () {
        var args = arguments;
        var reg = /\{(\d+)}/g;
        return this.replace(reg, function (g0, g1) {
            return args[+g1];
        });
    };
    var str = "this is :{0} and {1} , and  {0}".format("arg1","arg2")
    var main = "Scripts/main";
    ...