Skip to content

Latest commit

 

History

History
48 lines (33 loc) · 1.32 KB

prefer-string-starts-ends-with.md

File metadata and controls

48 lines (33 loc) · 1.32 KB

Prefer String#startsWith() & String#endsWith() over RegExp#test()

💼 This rule is enabled in the ✅ recommended config.

🔧💡 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.

Fail

const foo = /^bar/.test(baz);
const foo = /bar$/.test(baz);

Pass

const foo = baz.startsWith('bar');
const foo = baz.endsWith('bar');
const foo = baz?.startsWith('bar');
const foo = (baz ?? '').startsWith('bar');
const foo = String(baz).startsWith('bar');
const foo = /^bar/i.test(baz);