src/content/docs/linter/rules/use-regex-literals.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.3.0` - Diagnostic Category: [`lint/complexity/useRegexLiterals`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`prefer-regex-literals`](https://eslint.org/docs/latest/rules/prefer-regex-literals){
"linter": {
"rules": {
"complexity": {
"useRegexLiterals": "error"
}
}
}
}
Enforce the use of the regular expression literals instead of the RegExp constructor if possible.
There are two ways to create a regular expression:
/abc/u.new RegExp("abc", "u") .The constructor function is particularly useful when you want to dynamically generate the pattern, because it takes string arguments.
Using regular expression literals avoids some escaping required in a string literal, and are easier to analyze statically.
new RegExp("abc", "u");
/abc/u;
new RegExp("abc", flags);