在 javascript 文本字符串中插入像 \u1d6fc 这样的 unicode

insert unicode like \u1d6fc in a javascript text string

我正在编写一些代码来扫描字符串中的 TeX 风格希腊字符(如 \Delta 或 \alpha),并用 Unicode 符号替换该字符串。它适用于非斜体希腊字符。问题是我想对小写字母使用数学斜体。这些代码长一位数。例如,字母 alpha 的代码是 1d6fc。当我将 \u1d6fc 放入我的字符串中时,它显示为匹配 \u1d6f 的字符(小写字母 m 叠加波浪号)后跟字母 c。如何强制 "correct" 读取代码?

对于超出 UTF-16 范围的字符,您必须使用 UTF-16 surrogate pairs。在您的特定情况下,您可以使用 0xD835 0xDEFC:

    console.log('\uD835\uDEFC')

Here is a handy pair calculator. If you don't have to worry about Internet Explorer, you can also use String.fromCodePoint(), which will deal with that mess for you. If you do have to worry about Internet Explorer, MDN has a polyfill for that method.

要生成一个 \u 超过 4 个十六进制数字的转义序列(代码点属于所谓的星体平面),您可以使用 Unicode code point escape 符号 \u{xxxxx}

console.log ('\u{1d6fc}');

或者您可以使用 0x 前缀符号以十六进制表示的代码点值调用 String.fromCodePoint

console.log (String.fromCodePoint (0x1d6fc));