Skip to content

Files

Latest commit

 

History

History
24 lines (17 loc) · 618 Bytes

prefer-character-class.md

File metadata and controls

24 lines (17 loc) · 618 Bytes

Pattern: Missing use of of character class

Issue: -

Description

Instead of single-character alternatives (e.g. (?:a|b|c)), character classes (e.g. [abc]) should be preferred.

The main reason for doing this is performance. Character classes don't require backtracking and are heavily optimized by the regex engine. On the other hand, alternatives are usually quite tricky to optimize.

Examples

/* eslint regexp/prefer-character-class: "error" */
/* ✓ GOOD */
var foo = /[abc]/
var foo = /(?:a|b)/

/* ✗ BAD */
var foo = /a|b|c/
var foo = /(a|b|c)c/
var foo = /.|abc/
var foo = /(\w|\d)+:/