函数处理不定数目的参数

Posted by admin | Posted in javascript | Posted on 05-07-2011

0

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];
    });
};
format("And the %1 want to know whose %2 you %3", "papers", "shirt", "wear");

上面的脚本就会返回”And the papers want to know whose shirt you wear” 。

Comments are closed.