No Regex Literal Flags
Disallow flags on regex literals so shared patterns stay reusable and callers add flags at the call site via new RegExp(pattern, flags).
Summary
The no-regex-literal-flags rule reports any regex literal that carries flags (e.g., /pattern/gi). Patterns defined without flags can be reused by multiple callers, each choosing the flags they need through new RegExp(pattern, flags).
Why Use This Rule?
- Keeps centralized patterns flag-free so they work in any context without assumptions.
- Moves flag decisions to the call site where the intent is clearest.
- Prevents accidental global or case-insensitive matching baked into shared patterns.
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. |
Autofix
Autofix is not available for this rule. Flagged code must be updated manually.
Troubleshooting
- Need a flag for a one-off use? — Import the flag-free pattern and wrap it with
new RegExp(pattern, flags)at the call site. - Conflicts with
no-regex-literals— The two rules complement each other. Useno-regex-literalsin application code to ban all inline literals, and useno-regex-literal-flagsin the patterns file to keep definitions flag-free.