Skip to content

Instantly share code, notes, and snippets.

@kangax
Created December 1, 2008 18:56
Show Gist options
  • Select an option

  • Save kangax/30804 to your computer and use it in GitHub Desktop.

Select an option

Save kangax/30804 to your computer and use it in GitHub Desktop.
/*
'My name is #{name=Earl}'.interpolate2(); // "My name is Earl"
'My name is #{name}'.interpolate2({ name: 'Earl2D2' }); // "My name is Earl2D2"
*/
String.prototype.interpolate2 = (function(){
var re = /#{([-\w]+)(?:=([-\w]+))?}/g;
return function(source) {
if (!source) source = { };
return this.replace(re, function(match, name, value) {
return (name in source ? source[name] : (value || ''));
})
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment