πPutout plugin helps with regexp
npm install @putout/plugin-regexp[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-regexp.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-regexp "npm"
> 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
- β
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;
`json`
{
"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"
}
}
`js`
const a = /(ab|ab)/;
`js`
const a = /(ab)/;
Checkout in:
- πPutout Editor.
- AST Explorer.
`js`
/\)|\(/g;
`js`
/[)(]/g;
> Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument
>
> (c) MDN
Checkout in πPutout Editor.
`js`
's'.replaceAll(/hello/, 's');
'abc'.matchAll(/./);
`js`
's'.replaceAll(/hello/g, 's');
'abc'.matchAll(/./g);
`js`
const a = new RegExp('hello', 'i');
`js`
const a = /hello/i;
> The startsWith() method determines whether a string begins with the characters of a specified string, returning true or false 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.
`js`
/^hello/.test(a);
`js`
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, returning true or false as appropriate.
>
> (c) MDN
RegExp is overkill for such a simple task as determining that string located at the end.
`js`
/hello$/.test(a);
`js`
a.endsWith('hello');
Linter | Rule | Fix
--------|-------|------------|
π Putout| regexp/apply-ends-with| β
π¦ TypeScript ESLint | prefer-string-starts-ends-with | β
`js`
'hello'.replace(/hello/, 'world');
`js`
'hello'.replace('hello', 'world');
Simplify code according to string-replace-all.
`js`
'hello'.replace(/hello/g, 'world');
`js`
'hello'.replaceAll('hello', 'world');
`js`
/(hello)/.test(str);
`js`
/hello/.test(str);
Checkout in πPutout Editor.
`js`
const cleanText = code.replaceAll(/[,;\(\)]/g, '');
`js`
const cleanText = code.replaceAll(/[,;()]/g, '');
Linter | Rule | Fix
--------|-------|------------|
π Putout| regexp/remove-useless-escape| β
β£ ESLint | no-useless-escape | β
`js`
const a = /^\.hello$/.test(str);
`js`
const a = str === '.hello';
Checkout in AST Explorer.
`js`
/[aaabb]/.test(str);
`js``
/[ab]/.test(str);
MIT