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);