regexInvisibleCharacters
Reports invisible characters in regex patterns that should use escape sequences instead.
✅ This rule is included in the ts logical presets.
Invisible characters like tabs, non-breaking spaces, zero-width spaces, and other whitespace characters are difficult to distinguish visually. When these characters appear literally in regex patterns, they can lead to confusion and hard-to-debug issues. Using explicit escape sequences makes the intent clear and the code more maintainable.
Regular spaces (U+0020) are allowed since they are commonly visible in most editors and fonts.
Examples
Section titled “Examples”// Tab character embedded in regex (invisible)const pattern = / /;// Non-breaking space embedded in regexconst pattern = / /;// Zero-width space embedded in regexconst pattern = //;const pattern = /\x09/;const pattern = /\xA0/;const pattern = /\u200B/;// Regular space is allowedconst pattern = / /;// Standard escape sequences 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 you have tooling that renders invisible characters distinctively, or if your codebase intentionally uses literal invisible characters in regex patterns for specific matching requirements, you might prefer to disable this rule. Some specialized text processing may require literal invisible character matching.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-invisible-character