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.
Examples
Section titled “Examples”Word Character Class
Section titled “Word Character Class”const pattern = /[0-9a-zA-Z_]+/;const pattern = /\w+/;Negated Word Character Class
Section titled “Negated Word Character Class”const pattern = /[^0-9a-zA-Z_]/;const pattern = /\W/;With Ignore Case Flag
Section titled “With Ignore Case Flag”When the i flag is used, [a-z] already covers both lowercase and uppercase letters:
const pattern = /[0-9a-z_]/i;const pattern = /\w/i;RegExp Constructor
Section titled “RegExp Constructor”const pattern = new RegExp("[0-9a-zA-Z_]+");const pattern = new RegExp("\\w+");Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/prefer-w