javascript中的string.format

<script type="text/javascript">


String.prototype.format = function () {
    var values = arguments;
    return this.replace(/([^\{])?\{(\d+)\}(?!\})/g, function ($0, prefix, pos) {
				//alert(pos);
		pos = parseInt(pos);
		return (prefix || "") + values[pos];

	});
}


//使用方法 
alert("name:{0}, sex:{1}".format("sss", "''(){'"));
alert("{0}, sex:{1}".format("sss", "''(){'"));


</script>

  

posted @ 2012-03-27 08:52  阿牛  阅读(276)  评论(0编辑  收藏  举报