Skip to content

Instantly share code, notes, and snippets.

@dschnare
Last active May 26, 2016 18:01
Show Gist options
  • Save dschnare/3886395 to your computer and use it in GitHub Desktop.
Save dschnare/3886395 to your computer and use it in GitHub Desktop.
Ruby-style string interpolation for JavaScript.
// 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);
}
});
}
@SeriousM
Copy link

SeriousM commented May 1, 2014

you should replace eval($1) with something smarter because it opens a big security hole.

@dschnare
Copy link
Author

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).

@dschnare
Copy link
Author

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