Skip to content

Latest commit

 

History

History
67 lines (42 loc) · 1.9 KB

no-useless-quantifier.md

File metadata and controls

67 lines (42 loc) · 1.9 KB
pageClass sidebarDepth title description since
rule-details
0
regexp/no-useless-quantifier
disallow quantifiers that can be removed
v0.10.0

regexp/no-useless-quantifier

💼 This rule is enabled in the ✅ plugin:regexp/recommended config.

🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

disallow quantifiers that can be removed

📖 Rule Details

This rule reports quantifiers that can trivially be removed without affecting the pattern.

This rule only fixes constant-one quantifiers (e.g. a{1}). All other reported useless quantifiers hint at programmer oversight or fundamental problems with the pattern.

Examples:

  • a{1}

    It's clear that the {1} quantifier can be removed.

  • (?:a+b*|c*)?

    It might not very obvious that the ? quantifier can be removed. Without this quantifier, that pattern can still match the empty string by choosing 0 many cs in the c* alternative.

  • (?:\b)+

    The + quantifier can be removed because its quantified element doesn't consume characters.

/* eslint regexp/no-useless-quantifier: "error" */

/* ✓ GOOD */
var foo = /a*/;
var foo = /(?:a|b?)??/;
var foo = /(?:\b|(?!a))*/;

/* ✗ BAD */
var foo = /a{1}/;
var foo = /(?:\b)+/;
var foo = /(?:a+b*|c*)?/;

🔧 Options

Nothing.

🚀 Version

This rule was introduced in eslint-plugin-regexp v0.10.0

🔍 Implementation