如果我想在ES6/ES2015 javascript中打印一个Unicode汉字,我可以这样做:
同样地,如果我想要把一个变量插入到模板字符串中,我可以这样做:
console.log(`\u{4eb0}`);
同样地,如果我想要把一个变量插入到模板字符串中,我可以这样做:
let x = "48b0";
console.log(`The character code is ${ x.toUpperCase() }.`);
然而,似乎我无法将这两者结合起来打印一个包含40个连续的Unicode中文字符的列表。以下代码不起作用:
for (let i = 0, firstCharCode = parseInt("4eb0", 16); i < 40; ++i) {
let hexCharCode = (firstCharCode + i).toString(16);
console.log(`\u{${ hexCharCode }}`); // generates SyntaxError
}
所以我想问是否有可能实现这个。
\u
分隔。因此,"\uD834\uDF06" 变成了String.fromCodePoint(0xD834, 0xDF06)
,但与文字不同,您不必这样做,因为String.fromCodePoint(0x1D306)
也可以工作,而"\u1D306"
显然不行。 - Benjamin Gruenbaum