简体   繁体   English

ES6:错误字符转义序列创建ASCII字符串

[英]ES6: Bad character escape sequence creating ASCII string

Here's my code: 这是我的代码:

let padded = "03";
ascii = `\u00${padded}`;

However, I receive Bad character escape sequence from Babel. 但是,我从Babel收到了Bad character escape sequence I'm trying to end up with: 我试图以:

\

in the ascii variable. ascii变量中。 What am I doing wrong? 我究竟做错了什么?

EDIT: 编辑:

Ended up with ascii = (eval('"\\\\u00\u0026#39; + padded + '"')); 结束于ascii = (eval('"\\\\u00\u0026#39; + padded + '"'));

What am I doing wrong? 我究竟做错了什么?

A unicode escape sequence is basically atomic. Unicode转义序列基本上是原子的。 You cannot really build one dynamically. 您无法真正动态地构建一个。 Template literals basically perform string concatenation, so your code is equivalent to 模板文字基本上执行字符串连接,因此您的代码等效于

'\00' + padded

It should be obvious now why you get that error. 现在应该很明显为什么会出现该错误。 If you want to get the corresponding unicode character you can instead use String.fromCodePoint or String.fromCharCode : 如果要获取相应的unicode字符,则可以改用String.fromCodePointString.fromCharCode

String.fromCodePoint(3)

If you want a string that literally contains the character sequence \ , then you just need to escape the escape character to produce a literal backslash: 如果您想要一个字面上包含字符序列\的字符串, \需要转义转义字符以产生一个文字反斜杠即可:

`\\u00${padded}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM