Skip to content

Rule proposal: Prefer RegExp#exec() over String#match() #283

Closed
@sindresorhus

Description

@sindresorhus

Those methods return identical result for regexes without the g flag.

From String#match on MDN:

If the regular expression does not include the g flag, returns the same result as RegExp.exec().

I would like to enforce the use of RegExp#exec instead String#match for consistency when there's no g flag in the regex, and because it's faster:

RegExp.prototype.exec is a lot faster than String.prototype.match, but that’s because they are not exactly the same thing, they are different, their difference is out of this article’s scope, see this question: Stack Overflow.


This rule was originally proposed for inclusion in ESLint, but it was eventually declined for ambiguity reasons because of lack type knowledge. Lack of type knowledge is luckily not a problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: new plugin ruleNew rule request for eslint-pluginhas prthere is a PR raised to close thispackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions