String.prototype.format = function() { var args = arguments; return this.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); }; "{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET") 输出: ASP is dead, but ASP.NET is alive! ASP {2}
让javascript中想C#中的string.Format()一样的功能。还有其余的好的方法吗。
不如直接用jquery
jquery直接有吗???????、
@荒野的呼唤: var text = "a{0}b{1}";
var text2= $.format(text, 1, 2);
alert(text2);
可以参考jquery.valdate中的实现
https://github.com/jzaefferer/jquery-validation/blob/master/jquery.validate.js
代码从185行开始