var format = function(f){var a=arguments;return(""+f).replace(/\{(?:(\d+)|(\w+))\}/g,function(s,i,p){return p&&a[1]?a[1][p]:a[i]})}; console.log(format("{1} and {2}", "apples", "pears")); // apples and pears console.log(format("{1} {1} {2}", "hip", "hooray")); // hip hip hooray console.log(format("{key}: {value}", { key: "life", value: 42 })); // life: 42 console.log(format("mixing {prop} and {2}", { prop: "property" }, "index")); // mixing property and index