string.format for javascript
Code:
String.prototype.format = function() {
var str = this;
for (var i = 0; i <= arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
str = str.replace(reg, arguments[i]);
}
return str;
};
Usage:
console.log("Hello, {0}, today is {1}!".format("Tom", "Monday"));
Written by Neo Choi
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Related Tags
#javascript
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#