Skip to content

regexLiterals

Use a regular expression literal when the pattern is static.

✅ This rule is included in the ts logical and logicalStrict presets.

When the pattern and flags are static strings, using a regex literal is more concise and avoids unnecessary constructor calls. Regex literals are also easier to read and cannot throw at runtime due to invalid patterns, since they are validated at parse time.

const
const pattern: RegExp
pattern
=
var RegExp: RegExpConstructor
(pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp
("abc");
const
const pattern: RegExp
pattern
= new
var RegExp: RegExpConstructor
new (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp
("abc", "gi");
const
const pattern: RegExp
pattern
=
var RegExp: RegExpConstructor
(pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp
("");

This rule is not configurable.

If you prefer to always use RegExp constructors for consistency, or if you are building patterns from static pieces that are easier to read as strings, you might prefer to disable this rule.

Made with ❤️‍🔥 around the world by the Flint team and contributors.