Skip to main content

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?

  1. Centralizes regex patterns so they are easier to find, review, and maintain.
  2. Prevents duplicate patterns scattered across the codebase.
  3. 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.

OptionTypeDefaultDescription
ignoreFilesstring[][]File patterns to skip.
regexFilestring''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 ignoreFiles option so the rule skips that file.
  • Want to use new RegExp() inline instead? — The rule only targets regex literals (/pattern/). Constructing a RegExp from an imported pattern string is allowed.