Inject parameters into a string

 * Formats a string with the given parameters. The string to format must have
 * placeholders that correspond to the index of the arguments passed and surrounded 
 * by curly braces (e.g. 'Some {0} string {1}').
 * @param {string} var_args The string to be formatted should be the first 
 *     argument followed by the variables to inject into the string
 * @return {string} The string with the specified parameters injected
format = function(var_args){
    var args =, 1);
    return var_args.replace(/\{(\d+)\}/g, function(m, i){
        return args[i];

format("Is it {0}->{1} or {1}->{0}?", "chicken", "egg") == "Is it chicken->egg or egg->chicken?"

No comments: