JSFiddle - React, Tailwind, and code Playground
by Riccardo Volpe
CSS
span#python_str {
color: red;
font-style: italic;
}
JavaScript
String.prototype.format = function () {
var str = this;
for (var i = 0; i < arguments.length; i++) {
str = str.replace('{' + i + '}', arguments[i]);
}
return str;
}
/* IN ALTERNATIVA (FONTE: http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format)
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
*/
bar1 = 'foobar';
bar2 = 'jumped';
bar3 = 'dog';
python_format = 'The lazy {2} {1} over the {0}'.format(bar1,bar2,bar3);
document.getElementById("demo").innerHTML = "JavaScript equivalent of Python's format() function:<br><span id='python_str'>" + python_format + "</span>";