js string format
<script type="text/javascript">
//V1 method
String.prototype.format = function()
{
var args = arguments;
return this.replace(/\{(\d+)\}/g,
function(m,i){
return args[i];
});
}
//V2 static
String.format = function() {
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;
}
var a = "I Love {0}, and You Love {1},Where are {0}! {4}";
alert(String.format(a, "You","Me"));
alert(a.format("You","Me"));
</script>
//V1 method
String.prototype.format = function()
{
var args = arguments;
return this.replace(/\{(\d+)\}/g,
function(m,i){
return args[i];
});
}
//V2 static
String.format = function() {
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;
}
var a = "I Love {0}, and You Love {1},Where are {0}! {4}";
alert(String.format(a, "You","Me"));
alert(a.format("You","Me"));
</script>