Skip to content

regexLiterals

Use a regular expression literal when the pattern is static.

✅ This rule is included in the ts logical 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 pattern = RegExp("abc");
const pattern = new RegExp("abc", "gi");
const pattern = 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 ❤️‍🔥 in Boston by Josh Goldberg and contributors.