Package Exports
- @putout/plugin-regexp
- @putout/plugin-regexp/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@putout/plugin-regexp) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@putout/plugin-regexp 
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 -DRules
- β 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-duplicates-from-character-class;
Config
{
"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-regexp": "on",
"regexp/remove-duplicates-from-character-class": "on"
}
}optimize
β Example of incorrect code
const a = /(ab|ab)/;β Example of correct code
const a = /(ab)/;apply-character-class
Checkout in:
- πPutout Editor.
- AST Explorer.
β Example of incorrect code
/\)|\(/g;β Example of correct code
/[)(]/g;apply-global-regexp-to-replace-all
Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument(c) MDN
Checkout in πPutout Editor.
β Example of incorrect code
's'.replaceAll(/hello/, 's');
'abc'.matchAll(/./);β Example of correct code
's'.replaceAll(/hello/g, 's');
'abc'.matchAll(/./g);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, 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.
β Example of incorrect code
/^hello/.test(a);β Example of correct code
a.startsWith('hello');Comparison
| Linter | Rule | Fix |
|---|---|---|
| π Putout | regexp/apply-starts-with |
β |
| π¦ TypeScript ESLint | prefer-string-starts-ends-with |
β |
apply-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.
β Example of incorrect code
/hello$/.test(a);β Example of correct code
a.endsWith('hello');Comparison
| Linter | Rule | Fix |
|---|---|---|
| π Putout | regexp/apply-ends-with |
β |
| π¦ TypeScript ESLint | prefer-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';remove-duplicates-from-character-class
β Example of incorrect code
/[aaabb]/.test(str);β Example of correct code
/[ab]/.test(str);License
MIT