/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* Python(ish) string formatting: * >>> format('{0}', ['zzz']) * "zzz" * >>> format('{x}', {x: 1}) * "1" */ function format(s, args) { var re = /\{([^}]+)\}/g; return s.replace(re, function(_, match){ return args[match]; }); }
URL: http://davedash.com/2010/11/19/pythonic-string-formatting-in-javascript/