Nodejs String Format format()

Here you can find the source of format()

Method Source Code

/*/*  w  w w  . jav  a  2  s.c om*/
 * We extend here the String functionality by providing a
 * format function that allows to do something similar to
 * printf. This helps to keep the code cleaner avoiding
 * a lot of string concatenations. It can be used as:
 *
 * "The value of {0} is {1}".format(someVar, someOtherVar).
 */
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;
};

Related

  1. format()
    String.prototype.format = function(){
      var ret = this.toString();
      for (var i = 0; i < arguments.length; i++) {
        ret = ret.replace("%s", arguments[i]);
      return ret;
    };
    
  2. format()
    String.prototype.format = function() {
        var newString = this;
        counter = 0;
        while (counter < arguments.length && newString.indexOf('{}') != -1) {
            newString = newString.replace('{}', arguments[counter++]);
        matches = newString.match(/{[0-9]+}/g);
        if (matches != null) {
            for (var i = 0; i < matches.length; i++) {
    ...
    
  3. format()
    String.prototype.format = function () {
        var txt = this;
        for (var i = 0; i < arguments.length; i++) {
            var exp = new RegExp('\\{' + (i) + '\\}', 'gm');
            txt = txt.replace(exp, arguments[i]);
        return txt;
    };
    if (!String.format) {
    ...
    
  4. format()
    function clearStorage() {
      localStorage.clear();
      setIcon();
    function setIcon() {
    String.prototype.format = function() {
      var formatted = this;
      for (var i = 0; i < arguments.length; i++) {
    ...
    
  5. format()
    String.prototype.format = String.prototype.f = 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 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) {
    ...
    
  7. format()
    String.prototype.format= function(){
           var args = arguments;
           return this.replace(/\{(\d+)\}/g,function(s,i){
             return args[i];
           });
    
  8. format()
    function log(s){ 
      if(console)
        console.log(s);
    function callStack(){
      log(new Error().stack);
    String.prototype.format = function() {
      var args = arguments;
    ...
    
  9. 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