方案1http://www.cnblogs.com/loogn/archive/2011/06/20/2085165.html
String.prototype.format =
function(args) {
var result = this;
if (arguments.length > 0) {
if (arguments.length == 1 && typeof (args) == "object") {
for ( var key in args) {
if(args[key]!=undefined){
var reg = new RegExp("({" + key + "})", "g");
result = result.replace(reg, args[key]);
}
}
}
else {
for ( var i = 0; i < arguments.length; i++) {
if (arguments[i] != undefined) {
var reg = new RegExp("({[" + i + "]})", "g");
result = result.replace(reg, arguments[i]);
}
}
}
}
return result;
}
var result = this;
if (arguments.length > 0) {
if (arguments.length == 1 && typeof (args) == "object") {
for ( var key in args) {
if(args[key]!=undefined){
var reg = new RegExp("({" + key + "})", "g");
result = result.replace(reg, args[key]);
}
}
}
else {
for ( var i = 0; i < arguments.length; i++) {
if (arguments[i] != undefined) {
var reg = new RegExp("({[" + i + "]})", "g");
result = result.replace(reg, arguments[i]);
}
}
}
}
return result;
}
//
两种调用方式
var template1="我是{0},今年{1}了";
var template2="我是{name},今年{age}了";
var result1=template1.format("loogn",22);
var result2=template2.format({name:"loogn",age:22});
// 两个结果都是"我是loogn,今年22了"
var template1="我是{0},今年{1}了";
var template2="我是{name},今年{age}了";
var result1=template1.format("loogn",22);
var result2=template2.format({name:"loogn",age:22});
// 两个结果都是"我是loogn,今年22了"
方案2http://www.cnblogs.com/senion/archive/2011/02/25/1964567.html
function stringFormat() {
if (arguments.length == 0)
return null;
var str = arguments[0];
for ( var i = 1; i < arguments.length; i++) {
var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
str = str.replace(re, arguments[i]);
}
return str;
}
StringFormat("&Type={0}&Ro={1}&lPlan={2}&Plan={3}&={4}&Id={5}&Id={6}", data1, data2, data3,data4, data5,data6,data7);
if (arguments.length == 0)
return null;
var str = arguments[0];
for ( var i = 1; i < arguments.length; i++) {
var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
str = str.replace(re, arguments[i]);
}
return str;
}
StringFormat("&Type={0}&Ro={1}&lPlan={2}&Plan={3}&={4}&Id={5}&Id={6}", data1, data2, data3,data4, data5,data6,data7);
方案3http://jonllen.iteye.com/blog/389267
相信做前端开发的朋友都受过这个折磨:连接HTML的时候被可恶的单引号、双引号搞得头昏脑胀。比如:
element.innerHTML = ‘<a href=”‘ + url + ‘” onclick=”alert(\” + msg + ‘\’);”>’ + text + ‘</a>’;
这里介绍一个字符串格式化函数:
String.format =
function(str) {
var args = arguments, re = new RegExp(”%([1-" + args.length + "])”, “g”);
return String(str).replace(
re,
function($1, $2) {
return args[$2];
}
);
};
调用方法很简单:
element.innerHTML = String.format(’<a href=”%1″ onclick=”alert(\’%2\’);”>%3</a>’, url, msg, text);
var args = arguments, re = new RegExp(”%([1-" + args.length + "])”, “g”);
return String(str).replace(
re,
function($1, $2) {
return args[$2];
}
);
};
调用方法很简单:
element.innerHTML = String.format(’<a href=”%1″ onclick=”alert(\’%2\’);”>%3</a>’, url, msg, text);
意思就是用第n个参数把%n替换掉。这样一来清晰多了吧。
其它思路
http://code.google.com/p/jquery-utils/wiki/StringFormat
通过jquery 扩展
本文转自火地晋博客园博客,原文链接:http://www.cnblogs.com/yelaiju/archive/2012/03/27/2419212.html,如需转载请自行联系原作者