Return to Snippet

Revision: 36871
at November 29, 2010 06:56 by metoikos


Initial Code
/* 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]; });
    }

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

Initial Description

                                

Initial Title
Python like string formatting in javascript

Initial Tags
regex, javascript, format, python

Initial Language
JavaScript