Skip to content

Latest commit

 

History

History
128 lines (86 loc) · 2.66 KB

prefer-d.md

File metadata and controls

128 lines (86 loc) · 2.66 KB
pageClass sidebarDepth title description since
rule-details
0
regexp/prefer-d
enforce using `\d`
v0.1.0

regexp/prefer-d

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

🔧 This rule is automatically fixable by the --fix CLI option.

enforce using \d

📖 Rule Details

This rule is aimed at using \d instead of [0-9] in regular expressions.

/* eslint regexp/prefer-d: "error" */

/* ✓ GOOD */
var foo = /\d/;
var foo = /\D/;

/* ✗ BAD */
var foo = /[0-9]/;
var foo = /[^0-9]/;

🔧 Options

{
  "regexp/prefer-d": [
    "error",
    {
        "insideCharacterClass": "d"
    }
  ]
}

insideCharacterClass

This option control how character class element equivalent to \d will be treated.

Note: This option does not affect character classes equivalent to \d. E.g. [\d], [0-9], and [0123456789] are unaffected. It also does not affect expression non-nested operands equivalent to \d. E.g. [\d&&x], and [\d--x] are unaffected.

  • insideCharacterClass: "d" (default)

    Character class element equivalent to \d will be reported and replaced with \d.

    /* eslint regexp/prefer-d: ["error", { insideCharacterClass: "d" }] */
    
    /* ✓ GOOD */
    var foo = /[\da-z]/;
    
    /* ✗ BAD */
    var foo = /[0-9a-z]/;
    var foo = /[0-9]/;
  • insideCharacterClass: "range"

    Character class element equivalent to \d will be reported and replaced with the range 0-9.

    /* eslint regexp/prefer-d: ["error", { insideCharacterClass: "range" }] */
    
    /* ✓ GOOD */
    var foo = /[0-9a-z]/;
    
    /* ✗ BAD */
    var foo = /[\da-z]/;
    var foo = /[0-9]/;
    
    /* Ignore */
    var foo = /[\d--0]/v;
    /* ✗ BAD */
    var foo = /[[\da-z]--0]/v;
  • insideCharacterClass: "ignore"

    Character class element will not be reported.

    /* eslint regexp/prefer-d: ["error", { insideCharacterClass: "ignore" }] */
    
    /* ✓ GOOD */
    var foo = /[\da-z]/;
    var foo = /[0-9a-z]/;
    
    /* ✗ BAD */
    var foo = /[0-9]/;
    var foo = /[[0-9a-z]--0]/v;

🚀 Version

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

🔍 Implementation