No Regex Literals
Disallow inline regex literal expressions so patterns are centralized in a shared patterns file and stay reusable across the codebase.
Summary
The no-regex-literals rule reports any regex literal expression (e.g., /^v/, /\d+/g). It is recommended to define patterns in a dedicated file (such as regex.ts) and import them where needed.
Why Use This Rule?
- Centralizes regex patterns so they are easier to find, review, and maintain.
- Prevents duplicate patterns scattered across the codebase.
- Encourages reusable, well-documented patterns with clear names.
Examples
Configuration
Leaving regexFile unset or empty keeps the rule enabled and reports inline patterns without naming a destination. A non-empty path resolves from ESLint's working directory and must identify an existing regular file, even when the current source has no regex violations.
Options
warning
ignoreFiles is an escape hatch for files where this rule genuinely does not apply. It is not intended for routine use.
| Option | Type | Default | Description |
|---|---|---|---|
ignoreFiles | string[] | [] | File names to skip. Supports a leading-* suffix (*.test.ts), an exact path, or a trailing path segment; ** globs are not supported. |
regexFile | string | '' | Existing centralized patterns file resolved from ESLint's working directory. An empty value leaves the destination unspecified. |
Autofix
Autofix is not available for this rule. Flagged code must be updated manually.
Troubleshooting
- Configured path does not resolve — Check the path from the directory where ESLint runs. The target must already exist as a regular file.
- No centralized file yet — Leave
regexFileunset or empty. The rule stays active and reports inline patterns with destination-neutral guidance. - Error fires inside the patterns file — Check that
regexFileresolves to that exact file. The configured target is skipped automatically. - Need additional exceptions — Use
ignoreFilesonly for files where the rule does not apply. The configuredregexFiledoes not need to be listed again. - Inline
new RegExp()is reported — Import a shared pattern and pass the reference to the constructor. Static inline string and template patterns are reported.