Skip to content

Instantly share code, notes, and snippets.

@theskumar
Created September 28, 2012 08:09
Show Gist options
  • Save theskumar/3798575 to your computer and use it in GitHub Desktop.
Save theskumar/3798575 to your computer and use it in GitHub Desktop.
js: jquery.substitute()
// Does {placeholder} substitution on a string.
// The object passed as the second parameter provides values to replace the {placeholder}s.
//
// usage:
// jQuery.substitute('{foo}', {foo:'123'});
jQuery.substitute = function(str, sub) {
return str.replace(/\{(.+?)\}/g, function($0, $1) {
return $1 in sub ? sub[$1] : $0;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment