@putout/plugin-regexp
v13.0.0
Published
๐Putout plugin helps with regexp
Downloads
77,748
Maintainers
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 -DRules
- โ apply-character-class;
- โ apply-ends-with;
- โ apply-global-regexp-to-replace-al;
- โ apply-literal-notation;
- โ apply-starts-with;
- โ convert-replace-to-replace-all;
- โ convert-to-string;
- โ optimize;
- โ remove-useless-group;
- โ remove-useless-regexp;
- โ remove-useless-escape;
- โ remove-duplicates-from-character-class;
Config
{
"rules": {
"regexp/apply-character-class": "on",
"regexp/apply-global-regexp-to-replace-all": "on",
"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-escape": "on",
"regexp/remove-useless-regexp": "on",
"regexp/remove-duplicates-from-character-class": "on"
}
}optimize
โ Example of incorrect code
const a = /(ab|ab)/;โ Example of correct code
const a = /(ab)/;apply-character-class
Checkout in:
โ Example of incorrect code
/\)|\(/g;โ Example of correct code
/[)(]/g;apply-global-regexp-to-replace-all
Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument(c) MDN
Checkout in ๐Putout Editor.
โ Example of incorrect code
's'.replaceAll(/hello/, 's');
'abc'.matchAll(/./);โ Example of correct code
's'.replaceAll(/hello/g, 's');
'abc'.matchAll(/./g);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, returningtrueorfalseas 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, returningtrueorfalseas 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-escape
Checkout in ๐Putout Editor.
โ Example of incorrect code
const cleanText = code.replaceAll(/[,;\(\)]/g, '');โ Example of correct code
const cleanText = code.replaceAll(/[,;()]/g, '');Comparison
Linter | Rule | Fix
--------|-------|------------|
๐ Putout| regexp/remove-useless-escape| โ
โฃ ESLint | no-useless-escape | โ
remove-useless-regexp
โ Example of incorrect code
const a = /^\.hello$/.test(str);โ Example of correct code
const a = str === '.hello';remove-duplicates-from-character-class
Checkout in AST Explorer.
โ Example of incorrect code
/[aaabb]/.test(str);โ Example of correct code
/[ab]/.test(str);License
MIT
