正则表达式中字符 class 中的十进制转义
Decimal escape in a character class in a regular expression
有人可以根据 ECMAScript 标准解释字符 class 内十进制转义的语义吗?
例如,下面的模式是什么意思?它应该抛出语法错误吗?
[]
以下是核心规范中的相关部分:
-
The production ClassEscape::DecimalEscape evaluates as follows:
- Evaluate DecimalEscape to obtain an EscapeValue E.
- If E is not a character, throw a SyntaxError exception.
- Let ch be E's character.
- Return the one-element CharSet containing the character ch.
-
The production DecimalEscape::DecimalIntegerLiteral evaluates as follows:
- Let i be the MV of DecimalIntegerLiteral.
- If i is zero, return the EscapeValue consisting of the character U+0000 (NULL).
- Return the EscapeValue consisting of the integer i.
The definition of “the MV of DecimalIntegerLiteral” is in 11.8.3.
- NOTE
If \ is followed by a decimal number n whose first digit is not 0, then the escape sequence is considered to be a backreference. It is an error if n is greater than the total number of left capturing parentheses in the entire regular expression. [=20=] represents the character and cannot be followed by a decimal digit.
在网络浏览器的附加功能中也提到了 ClassEscape
:
-
ClassEscape (21.2.2.19) includes the following additional evaluation rules:
The production ClassEscape::DecimalEscapebut only if … evaluates as follows:
- Evaluate DecimalEscape to obtain an EscapeValue E.
- Assert: E is a character.
- Let ch be E's character.
- Return the one-element CharSet containing the character ch.
我的主要问题是,我觉得规则 DecimalEscape
只有在识别 0
时才会产生一个字符(然后是 return 和 U+0000
),否则它returns 是一个整数,但在 Firefox 中使用 Javascript 控制台测试时我无法得到语法错误。
以下是我找到的一些结果:
// This is the only one I understand:
/[[=12=]]/.test("\x00") // true
// Now it gets strange
/[]/.test("\x01") // true
/[]/.test("\x02") // true
/[]/.test("\x03") // true
/[]/.test("\x04") // true
/[]/.test("\x05") // true
/[]/.test("\x06") // true
/[]/.test("\x07") // true
/[]/.test("\x08") // false
/[]/.test("\x09") // false
/[]/.test("\x0a") // false
/[]/.test("\x0b") // false
// This is not interpreted as `` and `0`
/[]/.test("0") // false
// Also, it's not a backreference
/((((((((((a))))))))))[]/.test("aa") // false
为什么 return true
达到 7 然后 false
?这应该与八进制无关。我会很感激一些澄清。
/[[=10=]]/.test("\x00")
...
/[]/.test("\x07")
returns true
因为转义整数在八进制表示法中被视为数字 (base 8).
显然,当您使用 8 和 9 而不是以 8 为基数时,就不可能再使用了。在这种情况下,反斜杠将被忽略。
/[]/.test("\x0a")
/[]/.test("\x0b")
return false
因为 </code> <em>(base 8)</em> 给出 8 <em>(base 10)</em>.</p>
<pre><code>/[]/.test("\x08")
/[]/.test("\x09")
将 return true
.
此行为是浏览器附加功能(兼容性)的一部分。本节 B.1.2 String Literals adds LegacyOctalEscapeSequence
to the production rules of EscapeSequence
。此规则定义了从 0 到 255 的代码单元的八进制转义 [=19=]
到 7
。
有人可以根据 ECMAScript 标准解释字符 class 内十进制转义的语义吗?
例如,下面的模式是什么意思?它应该抛出语法错误吗?
[]
以下是核心规范中的相关部分:
-
The production ClassEscape::DecimalEscape evaluates as follows:
- Evaluate DecimalEscape to obtain an EscapeValue E.
- If E is not a character, throw a SyntaxError exception.
- Let ch be E's character.
- Return the one-element CharSet containing the character ch.
-
The production DecimalEscape::DecimalIntegerLiteral evaluates as follows:
- Let i be the MV of DecimalIntegerLiteral.
- If i is zero, return the EscapeValue consisting of the character U+0000 (NULL).
- Return the EscapeValue consisting of the integer i.
The definition of “the MV of DecimalIntegerLiteral” is in 11.8.3.
- NOTE
If \ is followed by a decimal number n whose first digit is not 0, then the escape sequence is considered to be a backreference. It is an error if n is greater than the total number of left capturing parentheses in the entire regular expression. [=20=] represents the character and cannot be followed by a decimal digit.
在网络浏览器的附加功能中也提到了 ClassEscape
:
-
ClassEscape (21.2.2.19) includes the following additional evaluation rules:
The production ClassEscape::DecimalEscapebut only if … evaluates as follows:
- Evaluate DecimalEscape to obtain an EscapeValue E.
- Assert: E is a character.
- Let ch be E's character.
- Return the one-element CharSet containing the character ch.
我的主要问题是,我觉得规则 DecimalEscape
只有在识别 0
时才会产生一个字符(然后是 return 和 U+0000
),否则它returns 是一个整数,但在 Firefox 中使用 Javascript 控制台测试时我无法得到语法错误。
以下是我找到的一些结果:
// This is the only one I understand:
/[[=12=]]/.test("\x00") // true
// Now it gets strange
/[]/.test("\x01") // true
/[]/.test("\x02") // true
/[]/.test("\x03") // true
/[]/.test("\x04") // true
/[]/.test("\x05") // true
/[]/.test("\x06") // true
/[]/.test("\x07") // true
/[]/.test("\x08") // false
/[]/.test("\x09") // false
/[]/.test("\x0a") // false
/[]/.test("\x0b") // false
// This is not interpreted as `` and `0`
/[]/.test("0") // false
// Also, it's not a backreference
/((((((((((a))))))))))[]/.test("aa") // false
为什么 return true
达到 7 然后 false
?这应该与八进制无关。我会很感激一些澄清。
/[[=10=]]/.test("\x00")
...
/[]/.test("\x07")
returns true
因为转义整数在八进制表示法中被视为数字 (base 8).
显然,当您使用 8 和 9 而不是以 8 为基数时,就不可能再使用了。在这种情况下,反斜杠将被忽略。
/[]/.test("\x0a")
/[]/.test("\x0b")
return false
因为 </code> <em>(base 8)</em> 给出 8 <em>(base 10)</em>.</p>
<pre><code>/[]/.test("\x08")
/[]/.test("\x09")
将 return true
.
此行为是浏览器附加功能(兼容性)的一部分。本节 B.1.2 String Literals adds LegacyOctalEscapeSequence
to the production rules of EscapeSequence
。此规则定义了从 0 到 255 的代码单元的八进制转义 [=19=]
到 7
。