prefer-string-starts-ends-with
March 27, 2026 ยท View on GitHub
๐ Prefer String#startsWith() & String#endsWith() over RegExp#test().
๐ผ This rule is enabled in the following configs: โ
recommended, โ๏ธ unopinionated.
๐ง๐ก This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
Prefer String#startsWith() and String#endsWith() over using a regex with /^foo/ or /foo$/.
This rule is fixable, unless the matching object is known not a string.
Examples
// โ
const foo = /^bar/.test(baz);
// โ
const foo = baz.startsWith('bar');
// โ
const foo = /bar$/.test(baz);
// โ
const foo = baz.endsWith('bar');
// โ
const foo = /^bar/i.test(baz);