Nodejs String Format format()

Here you can find the source of format()

Method Source Code

define(function () {
    return {//from www . j a va  2s .  c  o  m
        currentDate: function () {
            var date = new Date();

            var dd = date.getDate();
            var mm = date.getMonth() + 1;
            var yyyy = date.getFullYear();
            var hh = date.getHours();
            var MM = date.getMinutes();
            var ss = date.getSeconds();

            return (hh < 10 ? '0' + hh : hh) + ':' + (MM < 10 ? '0' + MM : MM) + ':' + (ss < 10 ? '0' + ss : ss) + ' ' + (dd < 10 ? '0' + dd : dd) + '.' + (mm < 10 ? '0' + mm : mm) + '.' + yyyy;
        }
    }
});

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()
    'use strict';
    String.prototype.format = function () {
        var str = this;
        for (var i = 0; i < arguments.length; i++) {
            var regEx = new RegExp("\\{" + (i) + "\\}", "gm");
            str = str.replace(regEx, arguments[i]);
        return str;
    
  2. format()
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function (match, number) {
            if (typeof args[number] != 'undefined') {
                return args[number];
            return "";
        });
    };
    ...
    
  3. format()
    String.prototype.format = function () {
      var str = this;
      for ( var i = 0; i < arguments.length; i ++ ) {
        str = str.replace( '{' + i + '}', arguments[ i ] );
      return str;
    
  4. format()
    var testSuite = testSuite || {};
    String.prototype.format = function() {
        var formatted = this;
        for( var arg in arguments ) {
            formatted = formatted.replace("{" + arg + "}", arguments[arg]);
        return formatted;
    };
    var testSuite = function (tableResultId){
    ...
    
  5. 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;
    };
    
  6. format()
    String.prototype.format = function () {
        var o = Array.prototype.slice.call(arguments);
        return this.replace(/{([^{}]*)}/g,
            function (match, capture) {
                var r = o[capture];
                return (typeof r === 'string' || typeof r === 'number') ? r : match;
        );
    };
    ...
    
  7. format()
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/\{(\d+)\}/g, function(orig,num) {
        return typeof args[num] != 'undefined' ? args[num] : orig;
      });
    };
    
  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 placeholderREG = /\{(\d+)\}/g;
      var args = arguments;
      return this.replace(placeholderREG, function(holder, num) {
        return args[num];
      });