Last active
May 26, 2016 18:01
-
-
Save dschnare/3886395 to your computer and use it in GitHub Desktop.
Ruby-style string interpolation for 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
// Author: Darren Schnare | |
// Keywords: javascript,interpolation,string,ruby | |
// License: MIT ( http://www.opensource.org/licenses/mit-license.php ) | |
// Repo: https://gist.github.com/gists/3886395 | |
String.prototype.interpolate = function (o) { | |
return this.replace(/#\{(.+?)\}/g, function ($0, $1) { | |
with (o) { | |
return eval($1); | |
} | |
}); | |
} |
Agreed. At the time it was just a really quick idea I had for Ruby-like string interpolation. What would you replace eval($1)
with to make it more secure? I guess you could start by processing the expression for assignments or function calls so the expression can only contain an R value (without method calls).
Ah, forget I asked. Just noticed your fork. Thanks for the work!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you should replace
eval($1)
with something smarter because it opens a big security hole.