如何在 lodash 模板中设置自定义转义 RegExp?
How to I set a custom escape RegExp in a lodash template?
这是我正在尝试的:
_.template("Hello ___name___",{escape:/___(.*?)___/g})({name:"Steve"})
我期望的输出是:
Hello Steve
但我得到的是:
Uncaught TypeError: _.template(...) is not a function(anonymous function)
这是有道理的,因为:
_.template("Hello ___name___",{escape:/___(.*?)___/g})
返回字符串 Hello ___name___
而不是模板函数。
是the docs错了还是什么?
N.B。我不想覆盖全局 _.templateSettings
.
如果你使用版本2.*
(在这个版本second argument
是模板的数据),你可以试试这个
console.log(_.template("Hello ___name___", null, {escape:/___(.*?)___/g})({name:"Steve"}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.2/lodash.js"></script>
对于版本 3.*
,您的示例工作正常
console.log(_.template("Hello ___name___", {escape:/___(.*?)___/g})({name:"Steve"}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.8.0/lodash.js"></script>
这是我正在尝试的:
_.template("Hello ___name___",{escape:/___(.*?)___/g})({name:"Steve"})
我期望的输出是:
Hello Steve
但我得到的是:
Uncaught TypeError: _.template(...) is not a function(anonymous function)
这是有道理的,因为:
_.template("Hello ___name___",{escape:/___(.*?)___/g})
返回字符串 Hello ___name___
而不是模板函数。
是the docs错了还是什么?
N.B。我不想覆盖全局 _.templateSettings
.
如果你使用版本2.*
(在这个版本second argument
是模板的数据),你可以试试这个
console.log(_.template("Hello ___name___", null, {escape:/___(.*?)___/g})({name:"Steve"}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.2/lodash.js"></script>
对于版本 3.*
,您的示例工作正常
console.log(_.template("Hello ___name___", {escape:/___(.*?)___/g})({name:"Steve"}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.8.0/lodash.js"></script>