@putout/plugin-regexp

putout plugin helps with regexp

Usage no npm install needed!

<script type="module">
  import putoutPluginRegexp from 'https://cdn.skypack.dev/@putout/plugin-regexp';
</script>

README

@putout/plugin-regexp NPM version

🐊Putout plugin helps with Regular Expressions.

Install

npm i @putout/plugin-regexp -D

Rules

{
    "rules": {
        "regexp/apply-literal-notation": "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"
    }
}

regexp/optimize

❌ Example of incorrect code

const a = /(ab|ab)/;

✅ Example of correct code

const a = /(ab)/;

regexp/apply-literal-notation

❌ Example of incorrect code

const a = new RegExp('hello', 'i');

✅ Example of correct code

const a = /hello/i;

regexp/convert-to-string

❌ Example of incorrect code

'hello'.replace(/hello/, 'world');

✅ Example of correct code

'hello'.replace('hello', 'world');

regexp/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');

regexp/remove-useless-group

❌ Example of incorrect code

/(hello)/.test(str);

✅ Example of correct code

/hello/.test(str);

regexp/remove-useless-regexp

❌ Example of incorrect code

const a = /^\.hello$/.test(str);

✅ Example of correct code

const a = str === '.hello';

License

MIT