String.prototype.format = function () { var args = arguments; console.log('args: ', args); if (typeof args[0] === 'object') { var obj = args[0]; console.log(obj); return this.replace(/{([A-Za-z_]+)}/g, function (m, i) { console.log(m, i); return obj[i]; }); } else { return this.replace(/{(d+)}/g, function (m, i) { console.log(m, i); return args[i]; }); } } var x = 'abc{a}, {b}'.format({a:1, b:2}); console.log(x) console.log('this is {0} {1}, {2}'.format(10,22,33))