javascript - 提醒转义号码时出现奇怪的字符

标签 javascript

我发现当我转义从 0 到 7 开始的数字并提醒它们时,我得到一个奇怪的符号:

alert( "\0" ); // or \1, \2, \3...\7

Weird character

这仅在我发出警报时才可见,但 document.write 和 console.log 不会显示它。转义大于 7 的数字看起来没问题。我正在使用最新版本的 Chrome。为什么我会遇到这个奇怪的 Angular 色?谢谢。

最佳答案

section 7.8.4 中描述了一般如何解析字符串文字。

但是,您看到的行为在 Annex B.1.2 中进行了描述。本节介绍旧 ES 版本中的八进制转义序列,该序列似乎仍然受支持。结果字符定义如下:

  • The CV of OctalEscapeSequence :: OctalDigit [lookahead ∉ DecimalDigit] is the character whose code unit value is the MV of the OctalDigit.
  • The CV of OctalEscapeSequence :: ZeroToThree OctalDigit [lookahead ∉ DecimalDigit] is the character whose code unit value is (8 times the MV of the ZeroToThree) plus the MV of the OctalDigit.
  • The CV of OctalEscapeSequence :: FourToSeven OctalDigit is the character whose code unit value is (8 times the MV of the FourToSeven) plus the MV of the OctalDigit.
  • The CV of OctalEscapeSequence :: ZeroToThree OctalDigit OctalDigit is the character whose code unit value is (64 (that is, 82) times the MV of the ZeroToThree) plus (8 times the MV of the first OctalDigit) plus the MV of the second OctalDigit.

\x (\xx, \xxx) 仅当 x 是一个时才被解释为八进制序列八进制数字,即 07 之间。

所以,所有字符 \0 - \7 实际上都是 control characters 。较高的值指的是其他字符,例如 \101A

我无法告诉您为什么 alert 显示奇怪的字符,而控制台没有显示任何内容......这可能是一个实现细节。


有趣的事实:严格模式下不允许八进制转义序列。

有趣的事实#2: \0 实际上不是八进制转义序列,并且仍然可以在严格模式下工作,因为它有自己的生产规则(请参阅 section 7.8.4 ) 。 OTHA,\00 是一个八进制序列,在严格模式下会抛出错误。

关于javascript - 提醒转义号码时出现奇怪的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12791256/

相关文章:

javascript - 在浏览器中打开新选项卡时出现问题

javascript - 我无法将整个文件夹及其所有子目录和文件拖放到浏览器容器中

javascript - 如何在嵌套数组中实现乘法字段更新

javascript - rangeSelector 输入和导出按钮未对齐

javascript - 我如何使用 Chart.js 中的对象键迭代或从数组中获取所有对象

javascript - 您好,我需要知道如何在 OpenShift for Angular 的 RAILS REST 应用程序中启用 CORS

javascript - 如何让多个日期选择器与 altFields 一起工作?

javascript - 全局颜色变量

javascript - 在一段代码的每个跨度内替换文本

javascript - 在 Promises 中实现错误区分