// 这份代码片段基于Public Domain发布
String.format = function(tpl) {
var index = 1, items = arguments;
return (tpl || '').replace(/{(\w*)}/g, function(match, p1) {
return items[index++] || p1 || match;
});
};
String.prototype.format = function() {
var index = 0, items = arguments;
return this.replace(/{(\w*)}/g, function(match, p1) {
return items[index++] || p1 || match;
});
};
> 'test{}test{}'.format()
< "test{}test{}"
> 'test{}test{2}'.format('1')
< "test1test2"
> 'test{}test{2}'.format('1', '3')
< "test1test3"