Python-like string interpolation in JavaScript

/ Published in: JavaScript
Save to your folder(s)

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

Copy this code and paste it in your HTML
  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


RSS Icon Subscribe to comments

You need to login to post a comment.