@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