Posted By

glends on 06/21/10


javascript python String interpolation

Versions (?)

Python-like string interpolation in JavaScript

 / Published in: JavaScript

This is a script tweeted by @gregmuellegger. Example usage: Supports f("Hello %s.", ["World"]) and f("Hello %(name)s.", {name: "World"})

  1. function (s, dict) {
  2. var i = 0;
  3. return s.replace(/%(?:\(([^)]+)\))?[diouxXeEfFgGcrs]/g, function (dummy, v) { return dict[v || i++]; });
  4. }

Report this snippet  

You need to login to post a comment.