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
- Correct
- Incorrect
// Flag-free pattern in the patterns file.
export const PATTERN_DIGITS = /\d+/;
// Caller adds flags at the call site.
const allDigits = new RegExp(PATTERN_DIGITS, 'g');
// Flags baked into the pattern.
export const PATTERN_DIGITS = /\d+/g;
Configuration
Enable the rule in your ESLint flat config:
import { noRegexLiteralFlags } from '@cbnventures/nova/rules/eslint';
export default [
{
plugins: {
'@cbnventures/nova': {
rules: {
'no-regex-literal-flags': noRegexLiteralFlags,
},
},
},
rules: {
'@cbnventures/nova/no-regex-literal-flags': ['error'],
},
},
];
This rule has no additional options.
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.