$ npm install @putout/plugin-regexp
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
{
"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"
}
}
const a = /(ab|ab)/;
const a = /(ab)/;
const a = new RegExp('hello', 'i');
const a = /hello/i;
The
startsWith()
method determines whether a string begins with the characters of a specified string, returningtrue
orfalse
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.
/^hello/.test(a);
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, returningtrue
orfalse
as appropriate.(c) MDN
RegExp is overkill for such a simple task as determining that string located at the end.
/hello$/.test(a);
a.endsWith('hello');
Linter | Rule | Fix |
---|---|---|
π Putout | regexp/apply-ends-with |
β |
π¦ TypeScript ESLint | prefer-string-starts-ends-with |
β |
'hello'.replace(/hello/, 'world');
'hello'.replace('hello', 'world');
Simplify code according to string-replace-all.
'hello'.replace(/hello/g, 'world');
'hello'.replaceAll('hello', 'world');
/(hello)/.test(str);
/hello/.test(str);
const a = /^\.hello$/.test(str);
const a = str === '.hello';
MIT
© 2010 - cnpmjs.org x YWFE | Home | YWFE