regexExecutors
Reports String.prototype.match calls that can be replaced with RegExp.prototype.exec.
✅ This rule is included in the ts stylisticStrict presets.
Reports calls to String.prototype.match() that should use RegExp.prototype.exec() instead.
When a regular expression does not have the global flag (g), both methods behave identically, but exec() is clearer in intent and may be slightly faster.
Examples
Section titled “Examples”String Literal with Regex Literal
Section titled “String Literal with Regex Literal”const result = "something".match(/thing/);const result = /thing/.exec("something");Variable with Regex
Section titled “Variable with Regex”const text = "something";const result = text.match(/pattern/);const text = "something";const result = /pattern/.exec(text);RegExp Constructor
Section titled “RegExp Constructor”const result = "something".match(new RegExp("thing"));const result = new RegExp("thing").exec("something");Global Flag (Valid)
Section titled “Global Flag (Valid)”Using the global flag with match() is valid because it returns all matches:
const allMatches = "one two three".match(/\w+/g);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you prefer consistent use of String.prototype.match() regardless of whether the global flag is present, you may disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.