JS类似Java String.format的函数

时间:2014-05-26 10:49:23   收藏:0   阅读:356

String.prototype.format = String.prototype.f = function () {
var s = this,
i = arguments.length;

while (i--) {
s = s.replace(new RegExp(‘\\{‘ + i + ‘\\}‘, ‘gm‘), arguments[i]);
}
return s;
};

"chen {0} hua".format("zeng")  

 

 

JS类似Java String.format的函数,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!