このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

SyntaxError: octal escape sequences can't be used in untagged template literals or in strict mode code

JavaScript 例外 "octal escape sequences can't be used in untagged template literals or in strict mode code" は、厳格モードの文字列リテラルやタグなしテンプレートリテラル内で 8 進エスケープシーケンスが使用された場合に発生します。

エラーメッセージ

SyntaxError: Octal escape sequences are not allowed in strict mode. (V8-based)
SyntaxError: \8 and \9 are not allowed in strict mode. (V8-based)
SyntaxError: Octal escape sequences are not allowed in template strings. (V8-based)
SyntaxError: \8 and \9 are not allowed in template strings. (V8-based)
SyntaxError: octal escape sequences can't be used in untagged template literals or in strict mode code (Firefox)
SyntaxError: the escapes \8 and \9 can't be used in untagged template literals or in strict mode code (Firefox)
SyntaxError: The only valid numeric escape in strict mode is '\0' (Safari)

エラーの種類

SyntaxError

エラーの原因

\ に続いて、単一の 0 以外の任意の数値が続く形式の 文字列エスケープシーケンス は、非推奨となっています。文字をコードポイント値で表したい場合は、代わりに \x または \u エスケープシーケンスを使用しましょう。例えば、\1 の代わりに \x01\u0001 を使用します。

タグなしのテンプレートリテラルには、厳格モードであるかどうかにかかわらず、8 進数のエスケープシーケンスを含めることはできません。一方、タグ付きのテンプレートリテラルには、どの形のエスケープシーケンスでも含めることができ、その場合、タグ関数が受け取るテンプレート配列には undefined が格納されます。

8 進エスケープシーケンス

js
"use strict";

"\251";

// SyntaxError: octal escape sequences can't be used in untagged template literals or in strict mode code

有効な 8 進数の数値

8進数のエスケープシーケンスについては、代わりに16進数のエスケープシーケンスを使用することができます。

js
"\xA9";

エスケープシーケンスを解釈せずにソーステキストをそのまま表したい場合は、String.raw を使用してください。

js
String.raw`\251`; // 4 文字から成る文字列

関連情報