Skip to content

Instantly share code, notes, and snippets.

@azakordonets
Created January 22, 2016 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azakordonets/8b7596ee8afc05d0aceb to your computer and use it in GitHub Desktop.
Save azakordonets/8b7596ee8afc05d0aceb to your computer and use it in GitHub Desktop.
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