Package Exports
- @putout/plugin-regexp
- @putout/plugin-regexp/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@putout/plugin-regexp) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@putout/plugin-regexp 
Regular expressions are patterns used to match character combinations in strings.
(c) MDN
πPutout plugin helps with Regular Expressions.
Install
npm i @putout/plugin-regexp -D
Rules
- β apply-ends-with;
- β apply-literal-notation;
- β apply-starts-with;
- β convert-replace-to-replace-all;
- β convert-to-string;
- β optimize;
- β remove-useless-group;
- β remove-useless-regexp;
- β types.js;
Config
{
"rules": {
"regexp/apply-literal-notation": "on",
"regexp/apply-starts-with": "on",
"regexp/apply-ends-with": "on",
"regexp/optimize": "on",
"regexp/convert-to-string": "on",
"regexp/convert-replace-to-replace-all": "on",
"regexp/remove-useless-group": "on",
"regexp/remove-useless-regexp": "on"
}
}
optimize
β Example of incorrect code
const a = /(ab|ab)/;
β Example of correct code
const a = /(ab)/;
apply-literal-notation
β Example of incorrect code
const a = new RegExp('hello', 'i');
β Example of correct code
const a = /hello/i;
apply-starts-with
The
startsWith()
method determines whether a string begins with the characters of a specified string, returningtrue
orfalse
as appropriate.(c) MDN
RegExp is overkill for such a simple task as determining that string located at the beginning. Check it out in π Putout Editor.
β Example of incorrect code
/^hello/.test(a);
β Example of correct code
a.startsWith('hello');
Comparison
Linter | Rule | Fix |
---|---|---|
π Putout | regexp/apply-starts-with |
β |
π¦ TypeScript ESLint | prefer-string-starts-ends-with |
β |
apply-ends-with
The
startsWith()
method determines whether a string ends with the characters of a specified string, returningtrue
orfalse
as appropriate.(c) MDN
RegExp is overkill for such a simple task as determining that string located at the end.
β Example of incorrect code
/hello$/.test(a);
β Example of correct code
a.endsWith('hello');
Comparison
Linter | Rule | Fix |
---|---|---|
π Putout | regexp/apply-ends-with |
β |
π¦ TypeScript ESLint | prefer-string-starts-ends-with |
β |
convert-to-string
β Example of incorrect code
'hello'.replace(/hello/, 'world');
β Example of correct code
'hello'.replace('hello', 'world');
convert-replace-to-replace-all
Simplify code according to string-replace-all.
β Example of incorrect code
'hello'.replace(/hello/g, 'world');
β Example of correct code
'hello'.replaceAll('hello', 'world');
remove-useless-group
β Example of incorrect code
/(hello)/.test(str);
β Example of correct code
/hello/.test(str);
remove-useless-regexp
β Example of incorrect code
const a = /^\.hello$/.test(str);
β Example of correct code
const a = str === '.hello';
License
MIT