SyntaxError: invalid decimal escape in regular expression

JavaScript 異常 "invalid decimal escape in regular expression"(正則表示式中無效的十進位制轉義序列)發生在 支援 Unicode 的正則表示式模式中使用傳統的八進位制轉義序列時。

訊息

SyntaxError: Invalid regular expression: /\00/u: Invalid decimal escape (V8-based)
SyntaxError: invalid decimal escape in regular expression (Firefox)
SyntaxError: Invalid regular expression: invalid octal escape for Unicode pattern (Safari)

錯誤型別

SyntaxError

哪裡出錯了?

在正則表示式中,\0 後面跟著另一個數字是傳統的八進位制轉義序列。在模板字串和嚴格模式的字串字面量中禁止使用相同的語法。在正則表示式中,Unicode 感知模式(uv)會停用此特性。\0 跟著另一個數字是有效的轉義序列,表示空字元(U+0000)。

\ 後面跟著一個非零數字是反向引用,如果它沒有引用捕獲組,則在 Unicode 感知模式下是無效的;更多資訊請參見無效的標識轉義

示例

無效案例

js
/\00/u;
/\01/u;

有效情況

js
// If you want to match NULL followed by a digit, use a character class
/[\0]0/u;
// If you want to match a character by its character value, use \x
/\x01/u;

另見