首页 > 代码库 > String Format
String Format
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, i) {
return typeof args[i] != ‘undefined‘ ? args[i] : match;
});
};
String.format = function(template) {
if(0 == arguments.length) return null;
var args = Array.prototype.slice.call(arguments, 1);
return String.prototype.format.apply(template, args);
}
if(typeof jQuery != ‘undefined‘) {
jQuery.extend({
format: function(template) {
return String.format.apply(template, arguments);
}
});
}
// console.log(‘{0} {1}‘.format(‘hello‘, ‘world‘));
// console.log(String.format(‘{0} {1}‘, ‘hello‘, ‘world‘));
// console.log($.format(‘{0} {1}‘, ‘hello‘, ‘jQuery‘));
String Format