ECMAScript 2017:StringLiteral 中的 EscapeSequence

ECMAScript 2017: EscapeSequence in StringLiteral

以下摘录参考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 不会被解释为 line terminator。这两者似乎是矛盾的。如果它没有被解释为字符串文字内的换行符,那么它是如何解释的(如果有的话)?

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

我认为该部分的关键部分是 "appear literally",它表示字符串文字中的 \ 不会转换为结果字符串本身的反斜杠。这并不是说不允许使用反斜杠,而是说它们不允许 "appear literally".

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

您跳过了该引用的前面部分 "always contributes to the literal"。 \u000A 是完全允许的,并且 确实 被添加到字符串的内容中。该代码表示​​在词法语法的意义上它不被视为行终止符。就是说

var foo = "one\u000Atwo";

允许

var foo = "one
two";

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