Skip to content

Instantly share code, notes, and snippets.

@shaneriley
Created June 28, 2011 14:58
Show Gist options
  • Save shaneriley/1051316 to your computer and use it in GitHub Desktop.
Save shaneriley/1051316 to your computer and use it in GitHub Desktop.
Javascript String Interpolation
function interpolate(source, dictionary) {
return source.replace(/#{([^{}]*)}/g, function (a, b) {
var r = dictionary[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
});
}
// interpolate("#{first} #{last}", { first: "Shane", last: "Riley" });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment