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, 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.
/^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, returningtrueorfalseas 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