javascript format 字符串 函数

函数实现如下:

function format(string) {

    var args = arguments;

    var pattern = new RegExp("%([1-" + arguments.length + "])", "g");

    return String(string).replace(pattern, function (match, index) {

        return args[index];

    });

};

用法如下:

console.log(format("And the %1 want to know whose %2 you %3", "papers", "shirt", "wear"));

 

posted on 2014-04-01 21:29  Mr Code  阅读(505)  评论(0编辑  收藏  举报

导航