Skip to content

Instantly share code, notes, and snippets.

@klarstil
Created July 1, 2013 11:16
Show Gist options
  • Save klarstil/5900031 to your computer and use it in GitHub Desktop.
Save klarstil/5900031 to your computer and use it in GitHub Desktop.
Simple method which fills out the placeholders in a string
/**
* Formats a string and replaces the placeholders.
*
* @example format('<div class="%0"'>%1</div>, [value for %0], [value for %1], ...)
*
* @param {String} str
* @param {Mixed}
* @returns {String}
*/
var format = function (str) {
for (var i = 1; i < arguments.length; i++) {
str = str.replace('%' + (i - 1), arguments[i]);
}
return str;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment