Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This is a nice formatter for strings in javascript
+String.prototype.format = function (placeholders) {
+ var s = this;
+ for (var propertyName in placeholders) {
+ var re = new RegExp('{' + propertyName + '}', 'gm');
+ s = s.replace(re, placeholders[propertyName]);
+ }
+ return s;
console.log("Hi my name is ${name} and i'm {$age} years old".format({name: "Andrew", age: "30"}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment