js 实现php中sprintf函数,完成完美替换操作!
<script type="text/javascript">
//js 实现php sprintf
function sprintf(){
var arg = arguments,
str = arg[0] || '',
i, n;
for (i = 1, n = arg.length; i < n; i++) {
str = str.replace(/%s/, arg[i]);
}
return str;
}
var string = "hello %s";
string = sprintf(string,"world");
//alert(string); //hello world!
</script>