Format function that replace placeholders in strings with values - Node.js String

Node.js examples for String:Format

Description

Format function that replace placeholders in strings with values

Demo Code


/**//from w  ww .  j av  a 2s  .c  o m
 * Format function that replace placeholders in strings with values
 * Usage : "This is {0} formatted {1}".format("a", "string");
 **/
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 Tutorials