$ cnpm install @putout/plugin-regexp
Regular expressions are patterns used to match character combinations in strings.
(c) MDN
????Putout plugin helps with Regular Expressions.
npm i @putout/plugin-regexp -D
{
"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"
}
}
const a = /(ab|ab)/;
const a = /(ab)/;
Checkout in:
/\)|\(/g;
/[)(]/g;
Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument(c) MDN
Checkout in ????Putout Editor.
's'.replaceAll(/hello/, 's');
'abc'.matchAll(/./);
's'.replaceAll(/hello/g, 's');
'abc'.matchAll(/./g);
const a = new RegExp('hello', 'i');
const a = /hello/i;
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.
/^hello/.test(a);
a.startsWith('hello');
| Linter | Rule | Fix |
|---|---|---|
| ???? Putout | regexp/apply-starts-with |
✅ |
| ???? TypeScript ESLint | prefer-string-starts-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.
/hello$/.test(a);
a.endsWith('hello');
| Linter | Rule | Fix |
|---|---|---|
| ???? Putout | regexp/apply-ends-with |
✅ |
| ???? TypeScript ESLint | prefer-string-starts-ends-with |
✅ |
'hello'.replace(/hello/, 'world');
'hello'.replace('hello', 'world');
Simplify code according to string-replace-all.
'hello'.replace(/hello/g, 'world');
'hello'.replaceAll('hello', 'world');
/(hello)/.test(str);
/hello/.test(str);
Checkout in ????Putout Editor.
const cleanText = code.replaceAll(/[,;\(\)]/g, '');
const cleanText = code.replaceAll(/[,;()]/g, '');
| Linter | Rule | Fix |
|---|---|---|
| ???? Putout | regexp/remove-useless-escape |
✅ |
| ⏣ ESLint | no-useless-escape |
❌ |
const a = /^\.hello$/.test(str);
const a = str === '.hello';
Checkout in AST Explorer.
/[aaabb]/.test(str);
/[ab]/.test(str);
MIT
Copyright 2013 - present © cnpmjs.org | Home |