8.0.0 β€’ Published 6 months ago

@putout/plugin-regexp v8.0.0

Weekly downloads
1,579
License
MIT
Repository
github
Last release
6 months ago

@putout/plugin-regexp NPM version

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, 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.

❌ Example of incorrect code

/^hello/.test(a);

βœ… Example of correct code

a.startsWith('hello');

Comparison

LinterRuleFix
🐊 Putoutregexp/apply-starts-withβœ…
πŸ¦• TypeScript ESLintprefer-string-starts-ends-withβœ…

apply-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.

❌ Example of incorrect code

/hello$/.test(a);

βœ… Example of correct code

a.endsWith('hello');

Comparison

LinterRuleFix
🐊 Putoutregexp/apply-ends-withβœ…
πŸ¦• TypeScript ESLintprefer-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

8.0.0

6 months ago

7.0.0

12 months ago

6.1.0

1 year ago

6.3.0

1 year ago

6.2.0

1 year ago

6.0.0

1 year ago

5.5.1

2 years ago

5.5.0

2 years ago

5.4.3

2 years ago

5.4.2

2 years ago

5.4.1

2 years ago

5.4.0

2 years ago

5.3.0

2 years ago

5.2.0

2 years ago

5.1.0

2 years ago

5.0.0

2 years ago

4.1.0

2 years ago

4.0.0

2 years ago

3.0.0

3 years ago

2.8.0

3 years ago

2.7.0

3 years ago

2.6.0

3 years ago

2.5.1

3 years ago

2.5.0

3 years ago

2.4.0

3 years ago

2.3.4

3 years ago

2.3.3

3 years ago

2.3.0

3 years ago

2.2.0

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago