String.prototype.format = function () {
var str = this
for (var i = 0; i < arguments.length; i++) {
var re = new RegExp('\\{' + i + '\\}', 'gm')
str = str.replace(re, arguments[i])
}
return str
}
调用:
var str = "我叫{0},我有{1}元"
str.format("Aton",100)//"我叫Aton,我有100元"
仿写.png
第二种:
var name = "Aton"
var money = 100
console.log(`我叫${name},我有${money}元`)
//我叫Aton,我有100元
网友评论