组合unicode字符串 [英] Combination unicode string

查看:148
本文介绍了组合unicode字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个像这样的字符串 const str ='\ u33d_rotation'。其中 \ u33 是3的代码。这只是一个简单的测试用例。但这给了我:

I am trying to create a string like this const str = '\u33d_rotation'. Where \u33 is the code for 3. This is just a simple test case. however this gives me:


SyntaxError:格式错误的Unicode字符转义序列

SyntaxError: malformed Unicode character escape sequence

无论如何组合unicode与普通?

Is there anyway to combo unicode with plain?

我试图将它全部转换成这样的unicode:

I tried to convert it all to unicode like this:

'\\u' + '3d_rotation'.split('').map(char => String.charCodeAt(char).toString(16)).join('\\u')

所以我现在有:

const str = '\u33\u64\u5f\u72\u6f\u74\u61\u74\u69\u6f\u6e'

但这也会出现同样的错误

but this also gives same error

推荐答案

Unicode转义序列由4个十六进制数字组成。

Unicode escape sequences consist of 4 hexadecimal digits.

例如:© \ u00A9 ,而不是 \ uA9

以下是通过代码点转义字符的各种方法(来源: MDN ) - 注意前两个使用 Latin-1 编码,因此不适合您的代码:

Here are the various way to escape characters via their code point (source: MDN) - note that the first two use Latin-1 encoding and are therefore unsuitable with your code:

\XXX
    The character with the Latin-1 encoding specified by up to three
    octal digits XXX between 0 and 377.
    For example, \251 is the octal sequence for the copyright symbol.

\xXX
    The character with the Latin-1 encoding specified by the two
    hexadecimal digits XX between 00 and FF.
    For example, \xA9 is the hexadecimal sequence for the copyright symbol.

\uXXXX
    The Unicode character specified by the four hexadecimal digits XXXX.
    For example, \u00A9 is the Unicode sequence for the copyright symbol.

\u{XXXXX}
    Unicode code point escapes.
    For example, \u{2F804} is the same as the simple Unicode escapes \uD87E\uDC04.

这篇关于组合unicode字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆