regexHexadecimalEscapes
Reports regex character escapes that can be expressed more consistently using hexadecimal escapes.
✅ This rule is included in the ts stylisticStrict presets.
Characters in the range 0x00-0xFF can be expressed using either hexadecimal escapes (\xNN) or Unicode escapes (\uNNNN, \u{N}).
Hexadecimal escapes are more concise for this range and provide a consistent representation.
This rule enforces the use of hexadecimal escapes for characters that can be represented in two hex digits.
Examples
Section titled “Examples”const pattern = /\u000a/;const pattern = /\u{a}/u;const pattern = /\u{00ff}/u;const pattern = /\x0a/;const pattern = /\xff/u;// Unicode escapes are fine for values > 0xFFconst pattern = /\u0100/u;// Control characters with standard escapes are fineconst pattern = /\t\n\r/;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your codebase has established conventions around using Unicode escapes for consistency with characters outside the 0x00-0xFF range, you might prefer to disable this rule. Some teams prefer the uniformity of always using Unicode escapes regardless of the character value.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/hexadecimal-escape
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.