jquery基于{0} {1} 的正则替换

function formatParams(source,params){
	$.each(params, function(i, n) {
		source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
	});
	return source;
}

//调用方法
var source = "Hello {0},I'm {1}";
var params= ["Lisa","Sunny"];

var msg = formatParams(source,params) //Hello Lisa,I'm Sunny

你可能感兴趣的:(jquery)