Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 组合unicode字符串_Javascript - Fatal编程技术网

Javascript 组合unicode字符串

Javascript 组合unicode字符串,javascript,Javascript,我正在尝试创建一个类似这样的字符串conststr='\u33d\u rotation'。其中\u33是3的代码。这只是一个简单的测试用例。然而,这给了我: SyntaxError:格式错误的Unicode字符转义序列 是否有将unicode与plain组合的方法 我尝试将其全部转换为unicode,如下所示: '\\u' + '3d_rotation'.split('').map(char => String.charCodeAt(char).toString(16)).join('\\

我正在尝试创建一个类似这样的字符串
conststr='\u33d\u rotation'
。其中
\u33
是3的代码。这只是一个简单的测试用例。然而,这给了我:

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

是否有将unicode与plain组合的方法

我尝试将其全部转换为unicode,如下所示:

'\\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'

但这也会产生相同的错误,Unicode转义序列由4个十六进制数字组成

例如:
\u00A9
,而不是
\uA9

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

\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转义序列

像这样:

const str='\u0033d_旋转';

console.log(str)
或者你可以使用
\xA9
@tech4him nope,那将是拉丁-1(好吧,好吧,©的拉丁-1和unicode碰巧是一样的)参见上面编辑的答案拉丁-1中的字符在unicode中不是都一样吗?当然,OP可能不想使用
\x
,因为它将你限制为只使用拉丁字符,但我相信如果这是您想要的,它会起作用。@tech4him重点是
String。charCodeAt()
(OP使用的)可以返回不适合1字节的代码点-例如:√".charCodeAt(0)。toString(16)给出“321a”-
\x
在其他方面非常好:)非常感谢Racil!这真的很有帮助!我接受了Georgiga,因为他有低谷。