Created
January 22, 2016 08:29
-
-
Save azakordonets/8b7596ee8afc05d0aceb to your computer and use it in GitHub Desktop.
This is a nice formatter for strings in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+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