Posted By

metoikos on 11/29/10


Tagged

regex javascript format python String


Versions (?)

Python like string formatting in javascript


 / Published in: JavaScript
 

URL: http://davedash.com/2010/11/19/pythonic-string-formatting-in-javascript/

  1. /* Python(ish) string formatting:
  2.   * >>> format('{0}', ['zzz'])
  3.   * "zzz"
  4.   * >>> format('{x}', {x: 1})
  5.   * "1"
  6.   */
  7. function format(s, args) {
  8. var re = /\{([^}]+)\}/g;
  9. return s.replace(re, function(_, match){ return args[match]; });
  10. }

Report this snippet  

You need to login to post a comment.