JavaScript String sprintf

JavaScript 1 1 Comment

A quick and easy function for sprintf-style JavaScript string replacement:

Edit: thanks imma!


  1. Hiya,
    I’ve compacted it down a little & removed shift & –
    no real reason, just because I enjoy this stuff, hope that’s ok :-)
    –immaterial
    function format(s) {
      var a=arguments;
      return s.replace(/%(\d+)/g,
        function(m,i){return a[i]} )}

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">