JavaScript中类似Python的字符串插值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript中类似Python的字符串插值相关的知识,希望对你有一定的参考价值。

This is a script [tweeted by @gregmuellegger](http://twitter.com/gregmuellegger/status/16618146398). 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. }

以上是关于JavaScript中类似Python的字符串插值的主要内容,如果未能解决你的问题,请参考以下文章