现在的位置: 首页 > 综合 > 正文

javascript中的string.format

2012年06月11日 ⁄ 综合 ⁄ 共 331字 ⁄ 字号 评论关闭
<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>

  

抱歉!评论已关闭.