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
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 patterns to skip. |
regexFile | string | '' | Path to centralized patterns file. |
Autofix
Autofix is not available for this rule. Flagged code must be updated manually.
Troubleshooting
- Error fires inside the patterns file — Add the filename to the
ignoreFilesoption so the rule skips that file. - Want to use
new RegExp()inline instead? — The rule only targets regex literals (/pattern/). Constructing aRegExpfrom an imported pattern string is allowed.