首页 > 其他 > 详细

String Format

时间:2014-11-18 16:13:45      阅读:248      评论:0      收藏:0      [点我收藏+]

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

原文:http://my.oschina.net/u/923974/blog/345971

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!