问题描述
我正在努力获取ES6模板文字以对其结果产生一个反斜杠。
I'm struggling to get an ES6 template literal to produce a single backslash it its result.
> `\s`
's'
> `\\s`
'\\s'
> `\\\s`
'\\s'
> `\\\\s`
'\\\\s'
> `\u005Cs`
'\\s'
已通过Node 8.9测试。 1和10.0.0,方法是检查节点REPL上的值(而不是使用 console.log
打印它)
Tested with Node 8.9.1 and 10.0.0 by inspecting the value at a Node REPL (rather than printing it using console.log
)
推荐答案
如果我对您的问题没问题, \\
怎么样?
If I get your question right, how about \\
?
我尝试使用 $ node -i
并运行
console.log(`\\`);
已成功输出反斜杠。请记住,输出也可能会转义,因此知道您成功获得反斜线的唯一方法是获取字符代码:
Which successfully output a backslash. Keep in mind that the output might be escaped as well, so the only way to know you are successfully getting a backslash is getting the character code:
const myBackslash = `\\`;
console.log(myBackslash.charCodeAt(0)); // 92
并确保您实际上没有得到 \\ \
(即双反斜杠),请检查长度:
And to make sure you are not actually getting \\
(i.e. a double-backslash), check the length:
console.log(myBackslash.length); // 1
这篇关于如何在ES6模板文字的输出中添加一个反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!