0%

js字符串格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
String.prototype.format = function(args) {
if (arguments.length == 0) {
return this;
}
var result = this;
if (arguments.length == 1 && typeof(args) == "object") {
for (var key in args) {
if (args[key] != undefined) {
var reg = new RegExp("({" + key + "})", "g");
result = result.replace(reg, args[key]);
}
}
} else {
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] != undefined) {
var reg = new RegExp("({)" + i + "(})", "g");
result = result.replace(reg, arguments[i]);
}
}
}
return result;
}

var template1 = "Hello, {0}!";
var template2 = "Hello, {name}!";
console.log(template1.format("Tom"));
console.log(template2.format({name: "Tom"}));