经常需要动态拼接html字符串,想到用类似于.net的string.format函数比较好,于是找了下,stackoverflow的代码:
if (!String.prototype.format) { String.prototype.format = function() { var str = this.toString(); if (!arguments.length) return str; var args = typeof arguments[0], args = (("string" == args || "number" == args) ? arguments : arguments[0]); for (arg in args) str = str.replace(RegExp("\{" + arg + "\}", "gi"), args[arg]); return str; } }