Skip to content

regexWordMatchers

Reports character classes that match word characters and could use \w or \W instead.

✅ This rule is included in the ts stylisticStrict presets.

JavaScript regular expressions support word matcher escape sequences \w and \W instead of [a-zA-Z0-9_] and [^a-zA-Z0-9_], respectively, for regular expressions. Those escape sequences are more concise than writing out the full character class sets.

This rule reports on any character sets that can be replaced by \w or \W word matchers.

const pattern = /[0-9a-zA-Z_]+/;
const pattern = /[^0-9a-zA-Z_]/;

When the i flag is used, [a-z] already covers both lowercase and uppercase letters:

const pattern = /[0-9a-z_]/i;
const pattern = new RegExp("[0-9a-zA-Z_]+");

This rule is not configurable.

If your codebase has an established convention of using explicit character classes for readability, or if you prefer consistency with patterns that use partial word character ranges (like [a-z0-9]), you might prefer to disable this rule.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.