ejs模板引擎如何渲染unicode字符串


嗯,是这样的,我有一个unicode字符串


 var str = "\u7e41\u661f";

在node中使用ejs + express时
node


 response.render('eg.ejs', {obj: {a: str}});

eg.ejs


 <script>
var obj = '<%= obj.a%>'
</script>

结果是


 <script>
var obj = '繁星'
</script>

我想要的是


 <script>
var obj = '\u7e41\u661f'
</script>

请问一下该怎么做?

node.js ejs JavaScript

ronson 9 years, 7 months ago
3quake answered 9 years, 7 months ago

使用escape
var obj = '繁星'
那么转成\u形式就这么写:
var str = escape(obj).replace(/%u/g,'\\u');
如果想反过来,就用一下unescape
unescape(str.replace(/\\u/g,'%u'))

雷纳德@天 answered 9 years, 7 months ago

Your Answer