@putout/plugin-regexp
Version:
πPutout plugin helps with regexp
214 lines (146 loc) β’ 5.32 kB
Markdown
[]: https://img.shields.io/npm/v/@putout/plugin-regexp.svg?style=flat&longCache=true
[]: https://npmjs.org/package/@putout/plugin-regexp"npm"
> Regular expressions are patterns used to match character combinations in strings.
>
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
π[**Putout**](https://github.com/coderaiser/putout) plugin helps with **Regular Expressions**.
```
npm i @putout/plugin-regexp -D
```
- β
[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](
- β
[types.js](
```json
{
"rules": {
"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-regexp": "on"
}
}
```
```js
const a = /(ab|ab)/;
```
```js
const a = /(ab)/;
```
> `Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument`
>
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Requires_global_RegExp)
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/8c759e3c8d19d660ddb01bda04d75c8b/f1d0f3a6d491f034308f5f3a375900b0a620a3b3).
```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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith)
**RegExp** is overkill for such a simple task as determining that string located at the beginning. Check it out in π [**Putout Editor**](https://putout.cloudcmd.io/#/gist/79e3e41c3491fb8c45fb03580e42ef20/89971d2d6528ee78df4c2a0d6271179560f76cc1).
```js
/^hello/.test(a);
```
```js
a.startsWith('hello');
```
Linter | Rule | Fix
--------|-------|------------|
π **Putout**| [`regexp/apply-starts-with`](https://github.com/coderaiser/putout/tree/master/packages/plugin-regexp#apply-starts-with)| β
π¦ **TypeScript ESLint** | [`prefer-string-starts-ends-with`](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/prefer-string-starts-ends-with.md#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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith)
**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`](https://github.com/coderaiser/putout/tree/master/packages/plugin-regexp#apply-ends-with)| β
π¦ **TypeScript ESLint** | [`prefer-string-starts-ends-with`](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/prefer-string-starts-ends-with.md#prefer-string-starts-ends-with) | β
```js
'hello'.replace(/hello/, 'world');
```
```js
'hello'.replace('hello', 'world');
```
Simplify code according to [string-replace-all](https://github.com/tc39/proposal-string-replaceall).
```js
'hello'.replace(/hello/g, 'world');
```
```js
'hello'.replaceAll('hello', 'world');
```
```js
/(hello)/.test(str);
```
```js
/hello/.test(str);
```
```js
const a = /^\.hello$/.test(str);
```
```js
const a = str === '.hello';
```
MIT