{"id":21981773,"url":"https://github.com/gmullerb/eslint-plugin-regex","last_synced_at":"2025-04-30T05:06:22.176Z","repository":{"id":42706749,"uuid":"239045411","full_name":"gmullerb/eslint-plugin-regex","owner":"gmullerb","description":"ESLint rules using Regular Expressions","archived":false,"fork":false,"pushed_at":"2023-11-16T04:42:57.000Z","size":476,"stargazers_count":38,"open_issues_count":4,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-30T05:06:07.320Z","etag":null,"topics":["eslint","eslint-plugin","eslint-regex","eslint-regex-rule","eslint-regular-expression","eslint-regular-expression-rule","eslint-rules","eslintplugin","regex","regex-rule","regexp","regular-expression","regular-expression-rule"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gmullerb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-08T00:17:44.000Z","updated_at":"2025-02-05T13:39:02.000Z","dependencies_parsed_at":"2024-06-18T16:44:05.726Z","dependency_job_id":"2be2379c-f338-47fc-be07-ec2dea2af088","html_url":"https://github.com/gmullerb/eslint-plugin-regex","commit_stats":{"total_commits":17,"total_committers":2,"mean_commits":8.5,"dds":0.05882352941176472,"last_synced_commit":"9373b88009f823bf5024fd868f1ccf2836c0a022"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmullerb%2Feslint-plugin-regex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmullerb%2Feslint-plugin-regex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmullerb%2Feslint-plugin-regex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmullerb%2Feslint-plugin-regex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gmullerb","download_url":"https://codeload.github.com/gmullerb/eslint-plugin-regex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251644842,"owners_count":21620634,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eslint","eslint-plugin","eslint-regex","eslint-regex-rule","eslint-regular-expression","eslint-regular-expression-rule","eslint-rules","eslintplugin","regex","regex-rule","regexp","regular-expression","regular-expression-rule"],"created_at":"2024-11-29T17:19:32.819Z","updated_at":"2025-04-30T05:06:22.153Z","avatar_url":"https://github.com/gmullerb.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cbr/\u003e\n  \u003ca href=\"https://eslint-plugin-regex.github.io/\"\u003e\u003cimg src=\"https://assets.gitlab-static.net/uploads/-/system/project/avatar/16770425/eslint-plugin-regex.png\" alt=\" \"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003ch1 align=\"center\"\u003eESLint rules using Regular Expressions\u003c/h1\u003e\n\n[![eslint-plugin-regex](https://badgen.net/badge/homepage/eslint-plugin-regex/blue)](https://eslint-plugin-regex.github.io/)\n[![eslint-plugin-regex](https://badgen.net/badge/npm%20pack/eslint-plugin-regex/blue)](https://www.npmjs.com/package/eslint-plugin-regex)\n[![ ](https://badgen.net/npm/v/eslint-plugin-regex)](https://www.npmjs.com/package/eslint-plugin-regex)\n[![ ](https://badgen.net/npm/node/eslint-plugin-regex)](https://www.npmjs.com/package/eslint-plugin-regex)\n[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](LICENSE.txt)\n[![ ](https://gitlab.com/gmullerb/eslint-plugin-regex/badges/master/coverage.svg)](https://gmullerb.gitlab.io/eslint-plugin-regex/coverage/index.html)\n[![Github repo](https://badgen.net/badge/icon/github?icon=github\u0026label)](https://github.com/gmullerb/eslint-plugin-regex)\n[![Gitlab repo](https://badgen.net/badge/icon/gitlab?icon=gitlab\u0026label)](https://gitlab.com/gmullerb/eslint-plugin-regex)\n\n__________________\n\n## Quick Start\n\n1 . Add dependencies:\n\n`package.json`:\n\n```json\n  \"engines\" : {\n    \"node\" : \"\u003e=6.0.0\"\n  },\n  \"devDependencies\": {\n    \"eslint\": \"\u003e=4.0.0\",\n    \"eslint-plugin-regex\": \"1.10.0\",\n```\n\n2 . Configure eslint:\n\nShort configuration:\n\n`.eslintrc.json`:\n\n```json\n{\n  \"plugins\": [\n    \"regex\"\n  ],\n  \"rules\": {\n    \"regex/invalid\": [\n      \"error\", [\n        \"invalidRegex1\",\n        \"invalidRegexN\"\n      ]\n    ],\n    \"regex/required\": [\n      \"error\", [\n        \"requiredRegex1\",\n        \"requiredRegexN\"\n      ],\n      \"ignoreFilesRegex\"\n    ]\n  }\n}\n```\n\nFiles will be checked for the absence of `invalidRegex1` and `invalidRegexN`, and for the presence of `requiredRegex1` and `requiredRegexN`, and files with name matching `ignoreFilesRegex` will not be checked.\n\nDetailed configuration:\n\n`.eslintrc.json`:\n\n```json\n{\n  \"plugins\": [\n    \"regex\"\n  ],\n  \"rules\": {\n    \"regex/invalid\": [\n      \"error\", [{\n          \"regex\": \"invalidRegex1\",\n          \"replacement\": \"newValue\"\n        }, {\n          \"id\": \"regexIdN\",\n          \"message\": \"errorMessageN\",\n          \"regex\": \"invalidRegexN\",\n          \"files\": {\n            \"ignore\": \"ignoreFilesRegexN\"\n          }\n        }\n      ]\n    ],\n    \"regex/required\": [\n      \"error\", [{\n          \"id\": \"regexId1\",\n          \"regex\": \"requiredRegex1\",\n          \"message\": \"errorMessage1\",\n          \"files\": {\n            \"inspect\": \"inspectFilesRegex1\"\n          }\n        }, {\n          \"regex\": \"requiredRegexN\",\n          \"files\": {\n            \"ignore\": \"ignoreFilesRegexA\",\n            \"inspect\": \"inspectFilesRegexZ\"\n          }\n        }\n      ]\n    ]\n  }\n}\n```\n\nFiles will be checked for:\n\n* The absence of `invalidRegex1` but if found it will be replaced with `newValue`.\n* The absence of `invalidRegexN` only in files with name not matching `ignoreFilesRegexN`, but if found `errorMessageN` will be shown.\n* The presence of `requiredRegex1` only in files with name matching `inspectFilesRegex1`, but if not found `errorMessage1` will be shown.\n* The presence of `requiredRegexN` only in files with name matching `inspectFilesRegexA` and not matching `ignoreFilesRegexZ`.\n\n__________________\n\n## Goals\n\nThe idea is to allow the creation of different eslint rules based on Regular Expressions in order to have some \"freedom\" to create quick ESLint custom rules.\n\n## Rules\n\nName                                                  | Fixable | Description\n----------------------------------------------------- | ------- | -----------\n[`regex/invalid`](docs/rules/invalid-regex-rule.md)   | Yes     | checks that specified patterns are not found\n[`regex/required`](docs/rules/required-regex-rule.md) | No      | checks that specified patterns are found\n\nEach rule defines a set of patterns:\n\n![Rules](.readme/rules.svg)\n\n### 📏 `regex/invalid`\n\nThis rule checks that specified patterns are not found in files, i.e. **Invalid patterns**.\n\n✏ Example of **incorrect** code for this rule:\n\n```javascript\n/* eslint regex/invalid: ['error', ['\"']] */\n\nconst text = 'Hello \"My Friend\"'\n```\n\nThe error message will reflect the exact location, e.g.:\n\n```bash\n/path/to/some.js\n 34:25  error  Invalid regular expression /\"/gm found  regex/invalid\n```\n\n✏ Example of **correct** code for this rule:\n\n```javascript\n/* eslint regex/invalid: ['error', ['\"']] */\n\nconst text = 'Hello \\'My Friend\\''\n```\n\n### 📏 `regex/required`\n\nThis rule looks for specific patterns that must be present in each file, i.e. **Required patterns**.\n\n✏ Example of **incorrect** code for this rule:\n\n```javascript\n/* eslint regex/required: [\"error\", [\"^// Copyright My Friend\"]] */\n\nconst text = 'Hello \"My Friend\"'\n```\n\nThe error message will point to the beginning of the file, e.g.:\n\n```sh\n/path/to/some.js\n 1:1  error  Required regular expression /^\\/\\/ Copyright My Friend/gm not found in file  regex/required\n```\n\n✏ Example of **correct** code for this rule:\n\n```javascript\n/* eslint regex/required: [\"error\", [\"^// Copyright My Friend\"]] */\n\n// Copyright My Friend\nconst text = 'Hello \"My Friend\"'\n```\n\n### Options\n\nBoth rule has two options:\n\n* **array** of patterns definitions to analyze. [REQUIRED]\n  * Each pattern definition can be 'Short' or 'Detailed'.\n* a **string** representing the regular expression for ignoring files for all patterns. [OPTIONAL]\n\n```json\n[\n  \"error\",\n  [\n    \"regex1\",\n    \"regexN\"\n  ],\n  \"ignoreFilesRegex\"\n]\n```\n\n#### The *string* representing the regular expression\n\nRemember, Slashes (`/`) are not required in the string that defines the regex,\n\ne.g. To get the following regex `/^(test|spec)$/`, define:\n\n* **`\"^(test|spec)$\"`**, when using `.eslintrc.js` or `.eslintrc.json`.\n\ne.g. To get the following regex `/\\bhttp:/`, define:\n\n* **`\"\\bhttp:\"`**, when using `.eslintrc.js`, or\n* **`\"\\\\bhttp:\"`**, when using `.eslintrc.json`. (backslash needs to be double in a json file)\n\ne.g. To get the following regex `/.*test\\.js/`, define:\n\n* **`\".*test\\.js\"`**, when using `.eslintrc.js`, or\n* **`\".*test\\\\.js\"`**, when using `.eslintrc.json`. (backslash needs to be double in a json file)\n\n#### Short pattern definition\n\nEach pattern is specified by just a **`string`** representing the regular expression, i.e. `\"regex\"`\n\n```json\n{\n  \"regex/invalid\": [\n    \"error\",\n    [\n      \"invalidRegex1\",\n      \"invalidRegexN\"\n    ]\n  ],\n  \"regex/required\": [\n    \"error\",\n    [\n      \"requiredRegex1\",\n      \"requiredRegexN\"\n    ]\n  ]\n}\n```\n\n#### Detailed pattern definition\n\nIt is specified by an `object`, with the following fields:\n\n* `regex`: A **required** `string` for `regex/required` and `regex/invalid` representing the **Regular expression to look for**. [REQUIRED]\n* `flags`: A combination of flags, `i`, `s` and/or `u`, to be used by the Regular Expression. [OPTIONAL]\n* `replacement` for `regex/invalid` [1]: [OPTIONAL]\n  * An optional `string` used to replace the **invalid** found pattern, or\n  * An optional `object` that establish how the **invalid** found pattern will be replaced:\n    * `function`: used to replace the **invalid** found pattern.\n      * It will receive 3 parameters: `text`, `captured` and `$`, that can be used as desired.\n      * It must return a `string` value, if not, return value will be ignored.\n      * Its definition must be only the body of the function.\n  * One must be defined, either the `string` or `function`.\n* `id`: An optional `string` representing the **Pattern Id**. [OPTIONAL]\n* `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]\n* `files`: An optional `object` specifying which files to analyze: [OPTIONAL]\n  * `ignore`: A `string` representing **Regular expression of the files to be ignored** when validating this specific pattern.\n  * `inspect`:  A `string` representing **Regular expression of the files to be inspected** when validating this specific pattern.\n\n```json\n{\n  \"id\": \"regexId\",\n  \"regex\": \"regex\",\n  \"flags\": \"isu\",\n  \"replacement\": \"replacementString\",\n  \"message\": \"errorMessage\",\n  \"files\": {\n    \"ignore\": \"ignoreFilesRegex\",\n    \"inspect\": \"inspectFilesRegex\"\n  }\n}\n```\n\n\u003e * `regex` is the **only** Required field.\n\u003e * When `ignore` and `inspect` are present, `ignore` takes precedence.\n\u003e * Global ignore file pattern, takes precedence over `files` patterns.\n\u003e\n\u003e [1] In order to fix issues `eslint` must be run with `--fix` option.\n\nUsing `message` is pretty useful since it will give a better understanding to the developer when an error happens:\n\ne.g. Given the following definition:\n\n```json\n{\n  \"regex\": \"someRegex\",\n  \"message\": \"The Useful Error MessagE\"\n}\n```\n\nthen shown error will be similar to:\n\n```sh\n/path/to/some.js\n 1:1  error  The Useful Error MessagE  regex/required\n```\n\nor\n\n```bash\n/path/to/some.js\n 34:25  error  The Useful Error MessagE  regex/invalid\n```\n\ninstead of\n\n```sh\n/path/to/some.js\n 1:1  error  Required regular expression /someRegex/gm not found in file  regex/required\n```\n\nor\n\n```bash\n/path/to/some.js\n 34:25  error  Invalid regular expression /someRegex/gm found  regex/invalid\n```\n\n##### Definition of the Function used to replace the *invalid* found pattern\n\nDefinition of the function must be done as a `string` in 1 line, and the following rules apply:\n\n* It must return a `string` value, if not, return value will be ignored, i.e. it will silently fail.\n* Its definition must be **only the body of the function**.\n  * For \"simple\" functions where the `return` is found at the beginning of the body of the function and the **exact** word *return* is not present, `return` can be omitted.\n* If the function has invalid Javascript code, the function will be ignored, i.e. it will silently fail.\n\nFunction will receive 3 parameters, to be used as desired:\n\n* `text`: a `string` with the value of the invalid text found.\n* `captured`: an `array` of strings with the values of the captured groups for the regex.\n* `$`: an `array` of strings, with the value of the invalid text found plus the values of the captured groups for the regex, i.e.\n  * `$[0]` = `text`: a `string` with the value of the invalid text found.\n  * `$[1..]` = `captured`: an `array` of strings with the values of the captured groups for the regex.\n    * `$[1]` = `captured[0]` and so on.\n  * It allows smaller definitions.\n\n**Using parameter `text`**\n\ne.g.\n\n```javascript\n  function(text, captured, $) {\n    return text.trim()\n  }\n```\n\n`\"return text.trim()\"` =\u003e only the body of the function + returns a `string` value based on `text`\n\nHaving the following rule in `.eslintrc.json`:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"\\\\serror\\\\w*\\\\s\",\n  \"replacement\": {\n    \"function\": \"return text.trim()\"\n  }\n}\n```\n\nor using `$`:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"\\\\serror\\\\w*\\\\s\",\n  \"replacement\": {\n    \"function\": \"return $[0].trim()\"\n  }\n}\n```\n\nthen, given:\n\n`example.js`\n\n```javascript\nconst exception = \" error19 \"\n```\n\nwhen linting with fix, the result will be:\n\n```javascript\nconst exception = \"error19\"\n```\n\nAs the body of the function is \"simple\", i.e. the `return` is found at the beginning of the body of the function, and besides, the word *return* is not present, then the definition could be done as:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"\\\\serror\\\\w*\\\\s\",\n  \"replacement\": {\n    \"function\": \"text.trim()\"\n  }\n}\n```\n\nor\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"\\\\serror\\\\w*\\\\s\",\n  \"replacement\": {\n    \"function\": \"$[0].trim()\"\n  }\n}\n```\n\n**Using parameter `captured`**\n\ne.g.\n\n`\"return captured[0]\"` =\u003e only the body of the function + returns a `string` value based on `captured`\n\nHaving the following rule in `.eslintrc.json`:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"\\\\serror(\\\\w*)\\\\s\",\n  \"replacement\": {\n    \"function\": \"return captured[0]\"\n  }\n}\n```\n\nor using `$`:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"\\\\serror(\\\\w*)\\\\s\",\n  \"replacement\": {\n    \"function\": \"return $[1]\"\n  }\n}\n```\n\nthen, given:\n\n`example.js`\n\n```javascript\nconst exception = \" error19 \"\n```\n\nwhen linting with fix, the result will be:\n\n```javascript\nconst exception = \"19\"\n```\n\nAs the body of the function is \"simple\", i.e. the `return` is found at the beginning of the body of the function, and besides, the word *return* is not present, then the definition could be done as:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"\\\\serror(\\\\w*)\\\\s\",\n  \"replacement\": {\n    \"function\": \"captured[0]\"\n  }\n}\n```\n\nor\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"\\\\serror(\\\\w*)\\\\s\",\n  \"replacement\": {\n    \"function\": \"$[1]\"\n  }\n}\n```\n\n**Using parameters `text` and `captured`**\n\ne.g.\n\n`\"return text + ' = ' + captured[0]  + ' + ' + captured[1] + ' = ' + (parseInt(captured[0]) + parseInt(captured[1]))\"` =\u003e only the body of the function + returns a `string` value based on `text` and `captured`\n\nHaving the following rule in `.eslintrc.json`:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"(\\\\d+)\\\\+(\\\\d+)\",\n  \"replacement\": {\n    \"function\": \"return text + ' = ' + captured[0]  + ' + ' + captured[1] + ' = ' + (parseInt(captured[0]) + parseInt(captured[1]))\"\n  }\n}\n```\n\nor using `$`:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"(\\\\d+)\\\\+(\\\\d+)\",\n  \"replacement\": {\n    \"function\": \"return $[0] + ' = ' + $[1]  + ' + ' + $[2] + ' = ' + (parseInt($[1]) + parseInt($[2]))\"\n  }\n}\n```\n\nor :\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"(\\\\d+)\\\\+(\\\\d+)\",\n  \"replacement\": {\n    \"function\": \"return text + ' = ' + $[1]  + ' + ' + $[2] + ' = ' + (parseInt($[1]) + parseInt($[2]))\"\n  }\n}\n```\n\nor :\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"(\\\\d+)\\\\+(\\\\d+)\",\n  \"replacement\": {\n    \"function\": \"return `${text} = ${captured[0]} + ${captured[1]} = ${parseInt($[1]) + parseInt($[2])}`\"\n  }\n}\n```\n\nthen, given:\n\n`example.js`\n\n```javascript\nconst sum = \"4+5\"\n```\n\nwhen linting with fix, the result will be:\n\n```javascript\nconst sum = \"4+5 = 4 + 5 = 9\"\n```\n\nAs the body of the function is \"simple\", i.e. the `return` is found at the beginning of the body of the function, and besides, the word *return* is not present, then the definition could be done as:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"(\\\\d+)\\\\+(\\\\d+)\",\n  \"replacement\": {\n    \"function\": \"text + ' = ' + $[1]  + ' + ' + $[2] + ' = ' + (parseInt($[1]) + parseInt($[2]))\"\n  }\n}\n```\n\nor :\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"(\\\\d+)\\\\+(\\\\d+)\",\n  \"replacement\": {\n    \"function\": \"`${text} = ${captured[0]} + ${captured[1]} = ${parseInt($[1]) + parseInt($[2])}`\"\n  }\n}\n```\n\n**When `return` keyword is required**\n\ne.g.\n\ne.g. `const result = text === 'superb' ? 'Superb' : text; return result` =\u003e only the body of the function + returns a `string` value based on `text`.\n\nSince the `return` is not found at the beginning of the body of the function, `return` cannot be omitted, then rule definition will be as usual:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"\\\\w+\",\n  \"replacement\": {\n    \"function\": \"const result = text === 'superb' ? 'Superb' : text; return result\"\n  }\n}\n```\n\n\u003e Some cases may use Comma operator, e.g. `\"function\": \"result = text === 'superb' ? 'Superb' : text, result\"`\n\ne.g. `return text === 'return' ? 'Return' : text` =\u003e only the body of the function + returns a `string` value based on `text`.\n\nSince the *exact* word *return* is present, this will **required** `return`, then rule definition will be as usual:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"\\\\w+\",\n  \"replacement\": {\n    \"function\": \"return text === 'return' ? 'Return' : text\"\n  }\n}\n```\n\nFollowing case does not required `return`:\n\ne.g. `return text === 'Return' ? 'RETURN' : text` =\u003e only the body of the function + returns a `string` value based on `text`.\n\nSince the **exact** word *return* is not present, this will allow the following rule definition to be:\n\n```json\n{\n  \"id\": \"regexIdN\",\n  \"regex\": \"\\\\w+\",\n  \"replacement\": {\n    \"function\": \"text === 'Return' ? 'RETURN' : text\"\n  }\n}\n```\n\n###### Debugging of the Replacement Function for *invalid* found pattern\n\n* It is possible to add `console` statements to print some information in the Replacement Function.\n\n```json\n{\n  \"regex\": \"\\\\serror(\\\\w*)\\\\s\",\n  \"replacement\": {\n    \"function\": \"const extract = captured[0]; console.log(extract); return extract\"\n  }\n}\n```\n\n##### RegExp Flags\n\nThe following flags can be add to the regex:\n\n* `i`: For case insensitive search.\n* `s`: To allow `.` to match newline characters.\n* `u`: To treat the regex as a sequence of unicode code points.\n\nTo define the flags to be used, employ the field `flags` in the detailed pattern:\n\n* A combination of flags can be used, e.g. `\"is\"`.\n  * Order of flags is irrelevant, e.g. `\"si\"`.\n* It's case insensitive, e.g. `\"iS\"`, `\"Is\"` and `\"IS\"` are the same.\n* Invalid flags will be reported as an error by eslint.\n\n\u003e By default, `\"gm\"` is always added by the engine (since It's required).\n\ne.g.\n\nHaving the following detailed pattern:\n\n```json\n{\n  \"regex\": \"invalid\",\n  \"flags\": \"i\"\n}\n```\n\n`Invalid`, `inValid`, `INvalid` or `INVALID` will match.\n\n### String to Regular expression conversion\n\nInternally, each string from the array will be converted into a Regular Expression with `global` and `multiline` options, e.g.:\n\n`\"someRegex\"` will be transformed into `/someRegex/gm`\n\n\u003e Remember that backslash needs to be double in strings of a json file, e.g. To get the following regex `/\\bhttp:/` define the following string `\"\\\\bhttp:\"`.\n\n### Empty Meta characters\n\nFor some special cases when using meta characters that may result in an empty match, e.g. `^`, eslint-plugin-regex will report only the first case found, and after that case is fixed, the following will be report, if present.\n\ne.g.\n\n```json\n{\n  \"regex\": \"^(?!(?:(feature|fix|docs|config|refactor|revert|test).*[\\\\.:]$)|(\\\\*\\\\s\\\\w.*\\\\.$)|$)\"\n}\n```\n\n`/path/to/some.js`:\n\n```text\nconfig(ALL):\n\n* Use eslint-plugin-regex for commit message linting\n* Use eslint-plugin-regex for commit message linting\n```\n\nWhen linting, `eslint-plugin-regex` will only report the first case:\n\n```bash\n/path/to/some.js\n 3:1  error  Invalid regular expression /^(?!(?:(feature|fix|docs|config|refactor|revert|test).*[\\\\.:]$)|(\\\\*\\\\s\\\\w.*\\\\.$)|$)/gm found  regex/invalid\n```\n\n4:1  error will not be reported until 3:1 is fixed.\n\n\u003e The issue is that having an empty match does not allow the regex engine to move forward.\n\n### Error report\n\nThe 'Short pattern definition' errors are reported with the following structure:\n\nGiven `someRegex`, the following message will be shown on error:\n\n```\nInvalid regular expression /someRegex/gm found\n```\n\nor\n\n```\nRequired regular expression /someRegex/gm not found in file\n```\n\nThe 'Detailed pattern definition' errors are reported with the following rules:\n\nA . If `message` is present then that **exact message is reported**.  \nB . If `id` is present then:\n\nGiven `\"id\": \"someRegexId\"`, the following message will be shown on error:\n\n```\nInvalid regular expression 'someRegexId' found\n```\n\nor\n\n```\nRequired regular expression 'someRegexId' not found in file\n```\n\nC . If neither `message` nor `id` is present then the 'Short pattern definition' error message is shown.\n\n\u003e * `message` takes precedence over `id`.  \n\u003e * Although `id` is a quick solution (and useful when creating and testing a rule), using `message` will give more information to the team about the issue.\n\n### Mixing\n\n#### Mixing pattern types\n\nIt is possible to use both type of definitions, 'Short pattern definition' with 'Detailed pattern definition', in the array of patterns.\n\n`.eslintrc.json`:\n\n```json\n{\n  \"plugins\": [\n    \"regex\"\n  ],\n  \"rules\": {\n    \"regex/invalid\": [\n      \"error\", [\n        \"invalidRegex1\",\n        \"invalidRegex2\",\n        {\n          \"regex\": \"invalidRegex3\",\n          \"message\": \"errorMessage1\",\n          \"files\": {\n            \"inspect\": \"inspectFilesRegex1\"\n          }\n        },\n        {\n          \"id\": \"regexIdN\",\n          \"regex\": \"invalidRegexN\",\n          \"files\": {\n            \"ignore\": \"ignoreFilesRegexN\"\n          }\n        }\n      ]\n    ]\n  }\n}\n```\n\n* `invalidRegex1` and `invalidRegex2` are 'Short pattern definition'.\n* `invalidRegex3` and `invalidRegexN` are 'Detailed pattern definition'.\n\n#### Mixing rules\n\n##### Mixing error levels\n\nRules names have synonyms:\n\n`regex/invalid` = `regex/invalid-warn` = `regex/invalid-error` = `regex/another-invalid` = `regex/other-invalid`.\n\n`regex/required` = `regex/required-warn` = `regex/required-error` = `regex/another-required` = `regex/other-required`.\n\n* Synonyms are just that, synonyms, do not imply any level of error, but some are provided to increase readability, e.g. `regex/invalid-warn` does not imply `warn` level.\n* This will allow to mix different error levels.\n\nIt is possible to set different error level: `error`, `warn` and `off`. For this use a synonym for the regex rule name:\n\n`.eslintrc.json`:\n\n```json\n{\n  \"plugins\": [\n    \"regex\"\n  ],\n  \"rules\": {\n    \"regex/invalid\": [\n      \"error\",\n      [\n        \"invalidRegex1\",\n        \"invalidRegexN\"\n      ]\n    ],\n    \"regex/required\": [\n      \"error\",\n      [\n        \"requiredRegex1\",\n        \"requiredRegexN\"\n      ]\n    ],\n    \"regex/invalid-error\": [\n      \"error\", [\n        \"invalidRegexA1\",\n        \"invalidRegexA2\",\n        {\n          \"regex\": \"invalidRegexA3\",\n          \"message\": \"errorMessage1\",\n          \"files\": {\n            \"inspect\": \"inspectFilesRegexA1\"\n          }\n        },\n        {\n          \"id\": \"regexIdN\",\n          \"regex\": \"invalidRegexN\",\n          \"files\": {\n            \"ignore\": \"ignoreFilesRegexAN\"\n          }\n        }\n      ],\n    ],\n    \"regex/invalid-warn\": [\n      \"warn\", [\n        \"invalidRegexB1\",\n        \"invalidRegexB2\",\n        {\n          \"regex\": \"invalidRegexB3\",\n          \"message\": \"errorMessage1\",\n          \"files\": {\n            \"inspect\": \"inspectFilesRegex1\"\n          }\n        },\n        {\n          \"id\": \"regexIdN\",\n          \"regex\": \"invalidRegexBN\",\n          \"files\": {\n            \"ignore\": \"ignoreFilesRegexN\"\n          }\n        }\n      ],\n    ],\n    \"regex/other-invalid\": [\n      \"off\", [\n        \"invalidRegexC1\",\n        \"invalidRegexC2\",\n        {\n          \"regex\": \"invalidRegexB3\",\n          \"message\": \"errorMessage1\",\n          \"files\": {\n            \"inspect\": \"inspectFilesRegexC1\"\n          }\n        },\n        {\n          \"id\": \"regexIdN\",\n          \"regex\": \"invalidRegexBN\",\n          \"files\": {\n            \"ignore\": \"ignoreFilesRegexCN\"\n          }\n        }\n      ],\n    ],\n    \"regex/required-warn\": [\n      \"warn\",\n      [\n        \"requiredRegexA1\",\n        \"requiredRegexAN\"\n      ]\n    ]\n  }\n}\n```\n\n* Rules with invalid patterns and `error` level: `regex/invalid` and `regex/invalid-error`.\n* Rules with invalid patterns and `off` level: `regex/other-invalid`.\n* Rules with required patterns and `error` level: `regex/required`.\n* Rules with required patterns and `warn` level: `regex/required-warn`.\n\n##### Custom set of regex rules\n\nCreating and Using a Custom Set of regex rules **requires using `js` files**.\n\n###### Named Regex Rules approach\n\nA regex rule can be named with a custom name. The Rule name can be anything that **includes `invalid`, `disuse`, `avoid`, `required` or `use`**, ignoring letter case, and with the restrictions of predefined names (`invalid`, `disuse`, `avoid`, `invalid-warn`, `invalid-error`, `another-invalid`, `other-invalid`, `required`, `use`, `required-warn`, `required-error`, `another-required` and `other-required`).\n\n* `regex/*invalid*`, `regex/*disuse*`  or `regex/*avoid*` for invalid patterns.\n* `regex/*required*` or `regex/*use*` for required patterns.\n\n![Mixed Rules](.readme/mixed-rules.svg)\n\n\u003e In the name `invalid`, `disuse` and `avoid` will take precedence over `required` and `use`, e.g. If custom regex rule name has both `avoid` and `use` in the name, then the respective regex patterns will be consider invalid patterns.\n\n**`addRegexRuleName` must be used to add the custom regex rule name to the set of `eslint-plugin-regex` rules.**\n\n```javascript\nconst { addRegexRuleName } = require('eslint-plugin-regex')\n\naddRegexRuleName('*invalid*')\naddRegexRuleName('*required*')\n```\n\n* If the custom regex rule name is already defined, then an error will be shown:\n\n```sh\nError: Cannot read config file: /path/to/.eslintrc.js\nError: \"SomeRuleName\" already defined as eslint-plugin-regex rule name\n```\n\n***Local Custom Regex rules***\n\nCreate a local `.eslintrc.js`:\n\n1 . Add rule name using `addRegexRuleName`.  \n2 . Define `eslint-plugin-regex` custom regex rule.\n\n```javascript\nconst { addRegexRuleName } = require('eslint-plugin-regex')\n\naddRegexRuleName('invalid-custom-890')\n\nmodule.exports = {\n  plugins: [ 'regex' ],\n  rules: {\n    'regex/invalid-custom-890': [\n      'error', [\n        {\n          regex: 'invalidRegexBN',\n          files: {\n            ignore: 'ignoreFilesRegexCN'\n          }\n        }\n      ]\n    ]\n  }\n}\n```\n\n***Custom Regex rules package***\n\nCreate a custom ESLint package and add the custom regex rules with a **\"unique\"** name for each regex rule defined in the package, so it can be use with other package of regex rules or local regex rules.\n\nCustom package `index.js`:\n\n```javascript\nconst { addRegexRuleName } = require('eslint-plugin-regex')\n\naddRegexRuleName('invalid-custom-890')\n\nmodule.exports = {\n  configs: {\n    'someRegexRule1': {\n      plugins: [ 'regex' ],\n      rules: {\n        'regex/invalid-custom-890': [\n          'error', [\n            {\n              regex: 'invalidRegexBN',\n              files: {\n                ignore: 'ignoreFilesRegexCN'\n              }\n            }\n          ]\n        ]\n      }\n    }\n  }\n}\n```\n\n* This custom package defines 1 rule named `regex/invalid-custom-890` with only 1 invalid pattern with `error` as a default error level.\n\n\u003e An online example can be checked at [`eslint-plugin-base-style-config`](https://github.com/gmullerb/base-style-config/tree/master/js#regex-rules).  \n\u003e For more information on how to create a custom ESLint package check [ESLint official documentation: Working with Plugins](https://eslint.org/docs/developer-guide/working-with-plugins)\n\nthen use it,\n\nSome project `.eslintrc.json`:\n\n```json\n  { \"extends\": [ \"plugin:the-eslint-plugin/someRegexRule1\",\n```\n\nto change the default error level set by the package:\n\n```json\n  {\n    \"extends\": [ \"plugin:the-eslint-plugin/someRegexRule1\" ],\n    \"rules\": {\n      \"regex/invalid-custom-890\": \"warn\"\n\n```\n\nmixing with other regex rules:\n\n```json\n{\n  \"extends\": [ \"plugin:the-eslint-plugin/someRegexRule1\" ],\n  \"rules\": {\n    \"regex/invalid-custom-890\": \"warn\",\n    \"regex/required\": [\n      \"error\",\n      [\n        \"requiredRegex1\",\n        \"requiredRegexN\"\n      ]\n    ],\n    \"regex/invalid-error\": [\n      \"error\", [\n        \"invalidRegexA1\",\n        \"invalidRegexA2\",\n        {\n          \"regex\": \"invalidRegexA3\",\n          \"message\": \"errorMessage1\",\n          \"files\": {\n            \"inspect\": \"inspectFilesRegexA1\"\n          }\n        },\n        {\n          \"id\": \"regexIdN\",\n          \"regex\": \"invalidRegexN\",\n          \"files\": {\n            \"ignore\": \"ignoreFilesRegexAN\"\n          }\n        }\n      ],\n    ],\n```\n\n***Advantages***\n\n* Using Named Regex rule name will allow to have a **set of different regex rule**:\n  * Each rule with totally different settings.\n    * Allow to mix different regular expressions.\n    * Allow to mix different error levels.\n    * etc.\n  * Easily create custom regex rules package.\n* When using Named Regex Rules, shown **errors will be even more specific**, e.g.:\n\n```javascript\nconst { addRegexRuleName } = require('eslint-plugin-regex')\n\naddRegexRuleName('required-custom-896')\n```\n\nthen, if an error happens, the output will be something similar to:\n\n```sh\n/path/to/some.js\n  1:1  error  Required regular expression /requiredRegex/gm not found in file  regex/required-custom-896\n```\n\ninstead of\n\n```sh\n/path/to/some.js\n  1:1  error  Required regular expression /requiredRegex/gm not found in file  regex/required\n```\n\n###### Import/Export approach\n\nCreate a custom npm package using either with `json` or `js` files and add the custom regex rules.\n\nCustom package `index.js`:\n\nwith complete rule definition:\n\n```javascript\nmodule.exports = {\n  regex: 'invalidRegexBN',\n  files: {\n    ignore: 'ignoreFilesRegexCN'\n  }\n}\n```\n\nor\n\n```javascript\nmodule.exports = {\n  regex: 'invalidRegexBN',\n}\n```\n\nor with only regex definition:\n\n```javascript\nmodule.exports = 'invalidRegexBN'\n```\n\nor with multiple complete rule definition:\n\n```javascript\nmodule.exports = {\n ruleName1: {\n    regex: 'invalidRegex1',\n    files: {\n      ignore: 'ignoreFilesRegex1'\n    }\n  },\n  ruleNameN: {\n    regex: 'invalidRegexN',\n    files: {\n      ignore: 'ignoreFilesRegexN'\n    }\n  }\n}\n```\n\nor\n\n```javascript\nmodule.exports = {\n  ruleName1: {\n    regex: 'invalidRegex1',\n  },\n  ruleNameN: {\n    regex: 'invalidRegexN',\n  }\n}\n```\n\nor with multiple only regex definition:\n\n```javascript\nmodule.exports = {\n    ruleName1: 'invalidRegex1',\n    ruleNameN: 'invalidRegexN'\n}\n```\n\nor using `json` files:\n\n```json\n{\n  \"regex\": \"invalidRegexBN\",\n  \"files\": {\n    \"ignore\": \"ignoreFilesRegexCN\"\n  }\n}\n```\n\nor\n\n```json\n{\n  \"regex\": \"invalidRegexBN\",\n}\n```\n\nor\n\n```json\n{\n \"ruleName1\": {\n    \"regex\": \"invalidRegex1\",\n    \"files\": {\n      \"ignore\": \"ignoreFilesRegex1\"\n    }\n  },\n  \"ruleNameN\": {\n    \"regex\": \"invalidRegexN\",\n    \"files\": {\n      \"ignore\": \"ignoreFilesRegexN\"\n    }\n  }\n}\n```\n\nor\n\n```json\n{\n  \"ruleName1\": {\n    \"regex\": \"invalidRegex1\",\n  },\n  \"ruleNameN\": {\n    \"regex\": \"invalidRegexN\",\n  }\n}\n```\n\nor\n\n```json\n{\n    \"ruleName1\": \"invalidRegex1\",\n    \"ruleNameN\": \"invalidRegexN\"\n}\n```\n\n\u003e Different approaches can be defined, these are only a glance.\n\u003e For more information on how to create a custom npm package check [Contributing packages to the registry](https://docs.npmjs.com/packages-and-modules/contributing-packages-to-the-registry)\n\nThen use the custom package:\n\nSome project `.eslintrc.js`:\n\n```javascript\n  import * as SomeESLintSetOfRegexRulesPackage1 from 'the-custom-package1'\n  import * as SomeESLintSetOfRegexRulesPackage2 from 'the-custom-package2'\n\n  module.exports = {\n    plugins: [\"regex\"],\n    rules: {\n      \"regex/invalid\": [\n        'error', [\n          SomeESLintSetOfRegexRulesPackage1.ruleName1,\n          SomeESLintSetOfRegexRulesPackage1.ruleNameN,\n          SomeESLintSetOfRegexRulesPackage2.ruleName1,\n          SomeESLintSetOfRegexRulesPackage2.ruleNameN\n        ]\n      ],\n```\n\nor using synonyms to mix error levels:\n\n```javascript\n  import * as SomeESLintSetOfRegexRulesPackage1 from 'the-custom-package1'\n  import * as SomeESLintSetOfRegexRulesPackage2 from 'the-custom-package2'\n\n  module.exports = {\n    plugins: [\"regex\"],\n    rules: {\n      'regex/invalid-error': [\n        'error', [\n          SomeESLintSetOfRegexRulesPackage1.ruleNameN,\n          SomeESLintSetOfRegexRulesPackage2.ruleName1\n        ]\n      ],\n      'regex/invalid-warn': [\n        'warn', [\n          SomeESLintSetOfRegexRulesPackage1.ruleName1,\n          SomeESLintSetOfRegexRulesPackage2.ruleNameN\n        ]\n      ]\n```\n\n### `regex/invalid` vs `regex/required`\n\nBoth rule were design with *binary* approach:\n\n* `regex/invalid`: pattern **is not present** =\u003e any presence of the *specific* pattern in a file is invalid.\n* `regex/required`: pattern **is present** =\u003e only 1 presence of the *specific* pattern in a file is required.\n\nArray of patterns represent different logical operation for each rule:\n\n* `regex/invalid`: **OR** =\u003e the presence in a file of *any* of the patterns defined in the *array* is invalid.\n* `regex/required`: **AND** =\u003e the presence in file of *all* of the patterns defined in the *array* is required.\n\n### Examples\n\nCheck:\n\n* [invalid-regex Basic rule tests](tests/lib/rules/invalid-regex-rule.e2e-test.js)\n* [invalid-regex Detailed rule tests](tests/lib/rules/invalid-regex-detailed-rule.e2e-test.js)\n* [required-regex Basic rule tests](tests/lib/rules/required-regex-rule.e2e-test.js)\n* [required-regex Detailed rule tests](tests/lib/rules/required-regex-detailed-rule.e2e-test.js)\n* [The set of Regex Rules of `eslint-plugin-base-style-config`](https://github.com/gmullerb/base-style-config/tree/master/js#regex-rules)\n\n__________________\n\n## Prerequisites\n\n* [`\"eslint\": \"\u003e=4.0.0\"`](https://www.npmjs.com/package/eslint).\n\n__________________\n\n## Evolution\n\n[`CHANGELOG.md`](CHANGELOG.md): contains the information about changes in each version, chronologically ordered ([Keep a Changelog](http://keepachangelog.com)).\n\n## Extending/Developing\n\n[Developing](js/.readme/developing.md)\n\n## Contributing\n\n* **Use it**.\n* **Share it**.\n* [Give it a Star](https://github.com/gmullerb/eslint-plugin-regex).\n* [Propose changes or improvements](https://github.com/gmullerb/eslint-plugin-regex/issues).\n* [Report bugs](https://github.com/gmullerb/eslint-plugin-regex/issues).\n\n## License\n\n[MIT License](LICENSE.txt)\n\n__________________\n\n## Remember\n\n* Use code style verification tools =\u003e Encourages Best Practices, Efficiency, Readability and Learnability.\n* Code Review everything =\u003e Encourages Functional suitability, Performance Efficiency and Teamwork.\n* If viable, Start testing early =\u003e Encourages Reliability and Maintainability.\n\n## Additional words\n\nDon't forget:\n\n* **Love what you do**.\n* **Learn everyday**.\n* **Learn yourself**.\n* **Share your knowledge**.\n* **Think different!**.\n* **Learn from the past, dream on the future, live and enjoy the present to the max!**.\n* **Enjoy and Value the Quest** (It's where you learn and grow).\n\nAt life:\n\n* Let's act, not complain.\n* Be flexible.\n\nAt work:\n\n* Let's give solutions, not questions.\n* Aim to simplicity not intellectualism.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmullerb%2Feslint-plugin-regex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgmullerb%2Feslint-plugin-regex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmullerb%2Feslint-plugin-regex/lists"}