bherrero
1/28/2016 - 6:46 PM

If you need to replace a string like sprintf for PHP does, you can use this function.

If you need to replace a string like sprintf for PHP does, you can use this function.

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;
};