Nodejs String Format format()

Here you can find the source of format()

Method Source Code

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]);
    }//from   ww w  . j  a  v  a2  s . c o  m
    return s;
}

String.prototype.format = function () {
    var s = this;
    for (var i = 0; i < arguments.length; i++) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        s = s.replace(reg, arguments[i]);
    }
    return s;
};
String.prototype.format.regex = new RegExp("{-?[0-9]+}", "g");

String.prototype.endsWith = function (suffix) {
    return (this.substr(this.length - suffix.length) === suffix);
}

String.prototype.startsWith = function (prefix) {
    return (this.substr(0, prefix.length) === prefix);
}

String.prototype.replaceAll = function (find, replace) {
    var str = this;
    while( str.indexOf(find) > -1 ){
        str = str.replace(find, replace);
    }
    return str;
}

Related

  1. 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) {
    ...
    
  2. 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");
    ...
    
  3. format()
    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]);
      return source;
    };
    Date.prototype.format = function (fmt) { 
    ...
    
  4. format()
    String.prototype.format = function() {
      var formatted = this;
      for (arg in arguments) {
        formatted = formatted.replace('{' + arg + '}', arguments[arg]);
      return formatted;
    };
    
  5. 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();
    ...
    
  6. 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";
    ...
    
  7. format()
    String.prototype.format = function(){    
      var re_all = /{(\d+)?(:(\d+)?(.\d+?f))?}/g;
      var re_index = /(\d+):/;
      var re_before = /(\d+)\./;
      var re_after = /\.(\d+)/;
      var cur = this;
      var matches = cur.match(re_all);
      if (arguments.length > matches){
        console.log("Too many arguments!");
    ...
    
  8. format()
    String.prototype.format = function() {
      var txt = this;
      var i = arguments.length;
      var replaceTokens = function(txt, key, value) {
        return txt.replace(new RegExp('\\{' + key + '\\}', 'gm'), value);
      };
      if(i > 0 && typeof(arguments[0]) == "object") { 
        for(var key in arguments[0]) {
          txt = replaceTokens(txt, key, arguments[0][key]);
    ...
    
  9. format()
    String.prototype.format = function() {
        var s = this,
            i = arguments.length;
        while (i--) {
            s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
        return s;
    };