Last active
September 13, 2016 09:21
-
-
Save culshaw/7648304b1e6008c069cf28913fab0655 to your computer and use it in GitHub Desktop.
javascript:Template literal parser for non es6 strings
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
function interpolate(text, literals) { | |
return text.replace(new RegExp(/\$\{(\w+)\}/, "g"), function() { | |
if(arguments[1] in literals) { | |
return literals[arguments[1]]; | |
} | |
throw new Error("Literal "+arguments[1]+" doesn't exist for text replacement"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In ES6 this would be parsed fine
An example