Demo for "string.format for Javascript"
Read the article at http://joQuery.com/2012/string-format-for-javascript
HTML
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">
<div class="well">
<h1 id="myLink"></h1>
</div>
JavaScript
String.format = function() {
// The string containing the format items (e.g. "{0}")
// will and always has to be the first argument.
var theString = arguments[0];
// start with the second argument (i = 1)
for (var i = 1; i < arguments.length; i++) {
// "gm" = RegEx options for Global search (more than one instance)
// and for Multiline search
var regEx = new RegExp("\\{" + (i - 1) + "\\}", "gm");
theString = theString.replace(regEx, arguments[i]);
}
return theString;
}
$(document).ready(function(){
// http://joQuery.com/2012/string-format-for-javascript
var url = 'http://joquery.com',
year = 2014,
titleEncoded = 'string-format-for-javascript',
title = 'string.format for Javascript';
var link = String.format('<a href="{0}/{1}/{2}" title="{3}">{3}</a>',
url, year, titleEncoded, title);
$("#myLink").append($(link));
});