Javascript String format(args) method

Description

Javascript String format(args) method


String.prototype.format = function (args) {
    var newStr = this;
    for (var key in args) {
        newStr = newStr.split('{' + key + '}').join(args[key]);
    }//from w  w  w  .  j  a va  2s . co m
    return newStr;
}

Javascript String format(args)

String.prototype.format = function (args) {
    var newStr = this;
    for (var key in args) {
        newStr = newStr.replace('{' + key + '}', args[key]);
    }/*from w  w w .j a  v a  2  s.c  o m*/
    return newStr;
}

Javascript String format(args)

String.prototype.format = function (args) {
  var newStr = this;
  for (var key in args) {
    newStr = newStr.replace(new RegExp("{" + key + "}","g"), args[key]);
  }//  w w w. j  a va  2  s.  c  o  m
  return newStr;
};

Javascript String format(args)

/*/*from   w  w w .  j a v a  2  s .com*/
 * String interpolation
 * Example: "Hello {0} {1} {0}".format("world", "and") returns "Hello world and world"
 */
String.prototype.format = function(args) {
 var s = '' + this
 for (var i = 0; i < args.length; i++) {
  var reg = new RegExp("\\{" + i + "\\}", "gm")
  s = s.replace(reg, args[i])
 }
 return s
}

Javascript String format(args)

//templating.js//  w ww.  j av  a 2 s.  c om
String.prototype.format = function(args) {
//var args = arguments;
  return this.replace(/{(\d+)}/g, function(match, number) { 
    return typeof args[number] != 'undefined'
      ? args[number]
      : match
    ;
  });
};

Javascript String format(args)

/**//w w w .  j a va2 s . c o m
 * http://stackoverflow.com/questions/4974238/javascript-equivalent-of-pythons-format-function
 */
String.prototype.format = function(args){
    var this_string = '';
    for (var char_pos = 0; char_pos < this.length; char_pos++) {
        this_string = this_string + this[char_pos];
    }

    for (var key in args) {
        var string_key = '{' + key + '}';
        this_string = this_string.replace(new RegExp(string_key, 'g'), args[key]);
    }
    return this_string;
};

Javascript String format(args)

String.prototype.format = function (args) {
    'use strict';
    var str = this;
    return str.replace(String.prototype.format.regex, function (item) {
        var intVal = parseInt(item.substring(1, item.length - 1));
        var replace;
        if (intVal >= 0) {
            replace = args[intVal];//from  w  ww . jav  a 2  s.c  o m
        } else if (intVal === -1) {
            replace = '{';
        } else if (intVal === -2) {
            replace = '}';
        } else {
            replace = '';
        }
        return replace;
    });
};
String.prototype.format.regex = new RegExp('{-?[0-9]+}', 'g');

Javascript String format(args)

/*/*from w ww . ja  v a  2 s  .com*/
 * String extension methods
 */
String.prototype.format = function(args) {
  return this.replace(/{([^{}]*)}/g, function (fullMatch, subMatch) {
    var value = args[subMatch];
    return (typeof value === 'string' || typeof value === 'number') ? value : fullMatch;
  });
};

Javascript String format(args)

/* taken from http://www.codeproject.com/Tips/201899/String-Format-in-JavaScript */

String.prototype.format = function (args) {
 var str = this;/*w w  w . ja v  a2s  . c o  m*/
 return str.replace(String.prototype.format.regex, function(item) {
  var intVal = parseInt(item.substring(1, item.length - 1));
  var replace;
  if (intVal >= 0) {
   replace = args[intVal];
  } else if (intVal === -1) {
   replace = "{";
  } else if (intVal === -2) {
   replace = "}";
  } else {
   replace = "";
  }
  return replace;
 });
};
String.prototype.format.regex = new RegExp("{-?[0-9]+}", "g");
// end of String.format()

Javascript String format(args)

'use strict';//from  www  .  j a v a  2s  . c o  m


String.prototype.format = function (args) {
  if (arguments.length > 0) {
    var result = this;
    if (arguments.length == 1 && typeof (args) == "object") {
      for (var key in args) {
        result = result.replace(new RegExp("({" + key + "})", "g"), args[key]);
      }
    }
    else {
      for (var i = 0; i < arguments.length; i++) {
        if (arguments[i] == undefined) {
          return "";
        }
        else {
          var reg = new RegExp("({[" + i + "]})", "g");
          result = result.replace(reg, arguments[i]);
        }
      }
    }
    return result;
  }
  else {
    return this;
  }
};

Javascript String format(args)

String.prototype.format = function (args) {
    if (arguments.length > 0) {
        var result = this;
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                var reg = new RegExp("({" + key + "})", "g");
                result = result.replace(reg, args[key]);
            }//from   w  w w . ja v a2 s.  com
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] == undefined) {
                    return "";
                }
                else {
                    var reg = new RegExp("({[" + i + "]})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
        return result;
    }
    else {
        return this;
    }
}



PreviousNext

Related