@putout/plugin-regexp v8.0.0
@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
{
"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
6 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago