gpt4 book ai didi

javascript - ECMAScript 2017 : EscapeSequence in StringLiteral

转载 作者:行者123 更新时间:2023-12-01 02:12:56 27 4
gpt4 key购买 nike

以下摘录引用ECMAScript 2017 .

10.1 源文本、语法

转义序列,如 \u000A,不会被解释为行终止符(即新行):

In string literals, regular expression literals, template literals and identifiers, any Unicode code point may also be expressed using Unicode escape sequences that explicitly express a code point's numeric value. Within a comment, such an escape sequence is effectively ignored as part of the comment.

ECMAScript differs from the Java programming language in the behaviour of Unicode escape sequences.

If the Unicode escape sequence \u000A occurs within a string literal in a Java program, it is interpreted as a line terminator, which is not allowed within a string literal.

A Unicode escape sequence occurring within a string literal in an ECMAScript program, always contributes to the literal and is never interpreted as a line terminator or as a code point that might terminate the string literal.

11.8.4 字符串文字

代码点可能在字符串文字中显示为转义序列,但反斜线 (\) 除外。

A string literal is zero or more Unicode code points enclosed in single or double quotes. Unicode code points may also be represented by an escape sequence. All code points may appear literally in a string literal except for the closing quote code points, U+005C (REVERSE SOLIDUS), U+000D (CARRIAGE RETURN), U+2028 (LINE SEPARATOR), U+2029 (PARAGRAPH SEPARATOR), and U+000A (LINE FEED). Any code points may appear in the form of an escape sequence.

问题

  1. 如果不允许 \ (11.8.4),转义序列如何出现在字符串文字中?
  2. 11.8.4。指出代码点可以表示为转义序列。 10.1 规定字符串文字内的转义序列 \u000A 不会被解释为行终止符。这两者看起来很矛盾。如果它不被解释为字符串文字内的换行符,那么它是如何解释的(如果有的话)?

最佳答案

How can an escape sequence occur inside a string literal, if \ is not allowed (11.8.4)?

我认为该部分的关键部分是“按字面意思显示”,也就是说字符串文字中的 \ 不会转换为结果字符串本身中的反斜杠。这并不是说反斜杠是不允许的,而是说它们不会“按字面意思出现”。

10.1 states that escape sequence \uu000A inside a string literal is not interpreted as a line terminator.

您跳过了那句话的前半部分“总是对文字有贡献”。 \u000A 是完全允许的,并且 确实 会被添加到字符串的内容中。该代码表示​​它不被视为词法语法意义上的行终止符。据说是这样的

var foo = "one\u000Atwo";

即使如此也是允许的

var foo = "one
two";

是一个语法错误。两者都尝试在单词之间使用换行代码点,但第一个是允许的,因为从词法分析器的 Angular 来看,它实际上并未被视为行终止符。

关于javascript - ECMAScript 2017 : EscapeSequence in StringLiteral,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49635180/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com