Skip to content

Latest commit

 

History

History
29 lines (20 loc) · 625 Bytes

prefer-string-starts-ends-with.md

File metadata and controls

29 lines (20 loc) · 625 Bytes

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

Prefer String#startsWith() and String#endsWith() over using a regex with /^foo/ or /foo$/.

This rule is fixable.

Fail

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

Pass

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