Skip to content

Instantly share code, notes, and snippets.

@clonn
Created May 2, 2012 11:17
Show Gist options
  • Save clonn/2575906 to your computer and use it in GitHub Desktop.
Save clonn/2575906 to your computer and use it in GitHub Desktop.
JavaScript string template replace function
function format(format) {
if (!FB.String.format._formatRE) {
FB.String.format._formatRE = /(\{[^\}^\{]+\})/g;
}
var values = arguments;
return format.replace(
FB.String.format._formatRE,
function(str, m) {
var
index = parseInt(m.substr(1), 10),
value = values[index + 1];
if (value === null || value === undefined) {
return '';
}
return value.toString();
}
);
}
format('{0}.facebook.com/{1}', 'www', 'login.php');
@clonn
Copy link
Author

clonn commented May 2, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment