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
const pattern: RegExp
pattern
= /[0-9a-zA-Z_]+/;
const
const pattern: RegExp
pattern
= /[^0-9a-zA-Z_]/;

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

const
const pattern: RegExp
pattern
= /[0-9a-z_]/i;
const
const pattern: RegExp
pattern
= new
var RegExp: RegExpConstructor
new (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
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 ❤️‍🔥 around the world by the Flint team and contributors.