Skip to content

Instantly share code, notes, and snippets.

@vermilion1
Last active December 16, 2015 04:49
Show Gist options
  • Save vermilion1/5379524 to your computer and use it in GitHub Desktop.
Save vermilion1/5379524 to your computer and use it in GitHub Desktop.
Substitute
/**
* Substitute {d} with passed arguments
* @example substitute('key is {0} and value is {1}', 'some_key', 'some_value');
* @param string {String} initial string
* @returns {String} modified string
*/
var substitute = function (string) {
if (!string) {
return '';
}
var values = _.rest(arguments);
var result = string;
_.each(values, function (value, index) {
var reg = new RegExp('\\{' + index + '\\}');
result = result.replace(reg, value);
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment