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"'
/) are not required in the string, e.g. To get the following regex /.*test\.js/ define the following string ".*test\.js" when using .eslintrc.js or ".*test\\.js" when using .eslintrc.json (backslash needs to de double in a json file).It is specified by just a regular expression string, i.e. "regex"
/) are not required in the string, e.g. To get the following regex /\bhttp:/ define the following string "\bhttp:" when using .eslintrc.js or "\\bhttp:" when using .eslintrc.json (backslash needs to de double in a json file)..eslintrc.json:
{
"plugins": [
"regex"
],
"rules": {
"regex/required": [
"error", [
"requiredRegex1",
"requiredRegexN"
],
".*test\\.js"
]
}
}
It is specified by an object, with the following fields:
regex: A required string for regex/required and regex/invalid representing the Regular expression to look for. [REQUIRED]flags: A combination of flags, i, s and/or u, to be used by the Regular Expression. [OPTIONAL]id: An optional string representing the Pattern Id. [OPTIONAL]message: An optional string specifying the Message to be shown when an error happens (invalid regex is found or required regex is not found). [OPTIONAL]files: An optional object specifying which files to analyze: [OPTIONAL]
ignore: A string representing Regular expression of the files to be ignored when validating this specific pattern.inspect: A string representing Regular expression of the files to be inspected when validating this specific pattern.{
"id": "regexId",
"regex": "regex",
"flags": "isu",
"message": "errorMessage",
"files": {
"ignore": "ignoreFilesRegex",
"inspect": "inspectFilesRegex"
}
}
regexis the only Required field. Slashes (/) are not required in the string, e.g. To get the following regex/\bhttp:/:
- when using
.eslintrc.js, define the following string"\bhttp:", or- when using
.eslintrc.json, define"\\bhttp:"(backslash needs to de double in a json file).- When
ignoreandinspectare present,ignoretakes precedence.- Global ignore file pattern, takes precedence over
filespatterns.
.eslintrc.json:
{
"plugins": [
"regex"
],
"rules": {
"regex/required": [
"error", [{
"id": "regexId1",
"regex": "requiredRegex1",
"files": {
"inspect": "inspectFilesRegex1"
}
}, {
"regex": "requiredRegexN",
"message": "errorMessageN",
"files": {
"ignore": "ignoreFilesRegexN",
"inspect": "inspectFilesRegexN"
}
}
]
]
}
}
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
Check:
eslint-plugin-base-style-config