eslint-plugin-regex

Looks for Required regular expressions that must be present in each file

Rule Details

This rule looks for Required regular expressions that must be present in each file.

Example of incorrect code for this rule:

/* eslint regex/required: ["error", ["^// Copyright My Friend"]] */

const text = 'Hello "My Friend"'

Example of correct code for this rule:

/* eslint regex/required: ["error", ["^// Copyright My Friend"]] */

// Copyright My Friend
const text = 'Hello "My Friend"'

Options

Short pattern definition

It is specified by just a regular expression string, i.e. "regex"

.eslintrc.json:

{
  "plugins": [
    "regex"
  ],
  "rules": {
    "regex/required": [
      "error", [
        "requiredRegex1",
        "requiredRegexN"
      ],
      ".*test\\.js"
    ]
  }
}

Detailed pattern definition

It is specified by an object, with the following fields:

{
  "id": "regexId",
  "regex": "regex",
  "flags": "isu",
  "message": "errorMessage",
  "files": {
    "ignore": "ignoreFilesRegex",
    "inspect": "inspectFilesRegex"
  }
}

.eslintrc.json:

{
  "plugins": [
    "regex"
  ],
  "rules": {
    "regex/required": [
      "error", [{
          "id": "regexId1",
          "regex": "requiredRegex1",
          "files": {
            "inspect": "inspectFilesRegex1"
          }
        }, {
          "regex": "requiredRegexN",
          "message": "errorMessageN",
          "files": {
            "ignore": "ignoreFilesRegexN",
            "inspect": "inspectFilesRegexN"
          }
        }
      ]
    ]
  }
}

String to Regular expression conversion

Internally, each string from the array will be converted into a Regular Expression with global and multiline options, e.g.:

"requiredRegex1" will be transformed into /requiredRegex1/gm

Examples

Check:

More information