regexRepeatQuantifiers
Reports consecutive identical elements in regular expressions that should use quantifiers.
✅ This rule is included in the ts stylistic presets.
Matching multiple consecutive characters in a regular expression can be done either by writing out that character repeatedly or using a quantifier.
Quantifiers keep the regex shorter and harder to miscount, and they make it obvious how many times a part repeats without visually scanning a long run of characters.
For example, writing a{5} is more concise and readable than aaaaa.
This rule reports runs of five or more identical elements in regular expressions and suggests replacing them with quantifiers.
Examples
Section titled “Examples”Repeated Characters
Section titled “Repeated Characters”const pattern = /aaaaa/;const pattern = /a{5}/;Escape Sequences
Section titled “Escape Sequences”const pattern = /\d\d\d\d\d/;const pattern = /\d{5}/;Character Classes
Section titled “Character Classes”const pattern = /[a-z][a-z][a-z][a-z][a-z]/;const pattern = /[a-z]{5}/;const pattern = /...../;const pattern = /.{5}/;RegExp Constructor
Section titled “RegExp Constructor”const pattern = new RegExp("aaaaa");const pattern = new RegExp("a{5}");Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your project has a style guide that prefers explicit repetition for clarity, or if you are programmatically generating regular expressions where repetition is easier to produce, you might want to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/prefer-quantifier