Skip to content

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.

const
const result: RegExpMatchArray | null
result
= "something".
String.match(matcher: {
[Symbol.match](string: string): RegExpMatchArray | null;
}): RegExpMatchArray | null (+1 overload)

Matches a string or an object that supports being matched against, and returns an array containing the results of that search, or null if no matches are found.

@parammatcher An object that supports being matched against.

match
(/thing/);
const
const text: "something"
text
= "something";
const
const result: RegExpMatchArray | null
result
=
const text: "something"
text
.
String.match(matcher: {
[Symbol.match](string: string): RegExpMatchArray | null;
}): RegExpMatchArray | null (+1 overload)

Matches a string or an object that supports being matched against, and returns an array containing the results of that search, or null if no matches are found.

@parammatcher An object that supports being matched against.

match
(/pattern/);
const
const result: RegExpMatchArray | null
result
= "something".
String.match(matcher: {
[Symbol.match](string: string): RegExpMatchArray | null;
}): RegExpMatchArray | null (+1 overload)

Matches a string or an object that supports being matched against, and returns an array containing the results of that search, or null if no matches are found.

@parammatcher An object that supports being matched against.

match
(new
var RegExp: RegExpConstructor
new (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp
("thing"));

Using the global flag with match() is valid because it returns all matches:

const
const allMatches: RegExpMatchArray | null
allMatches
= "one two three".
String.match(matcher: {
[Symbol.match](string: string): RegExpMatchArray | null;
}): RegExpMatchArray | null (+1 overload)

Matches a string or an object that supports being matched against, and returns an array containing the results of that search, or null if no matches are found.

@parammatcher An object that supports being matched against.

match
(/\w+/g);

This rule is not configurable.

If you prefer consistent use of String.prototype.match() regardless of whether the global flag is present, you may disable this rule.

Made with ❤️‍🔥 around the world by the Flint team and contributors.