Skip to content

stringStartsEndsWith

Reports regex patterns that can be replaced with endsWith or startsWith.

✅ This rule is included in the ts stylistic presets.

Regular expressions with simple ^ or $ anchors can be replaced with the more readable and performant startsWith() and endsWith() string methods.

This rule detects regex .test() calls that check for simple string prefixes or suffixes and suggests using the dedicated string methods instead.

const hasPrefix = /^foo/.test(str);
const hasSuffix = /bar$/.test(str);

This rule is not configurable.

If you need case-insensitive matching or other regex features, the string methods won’t work as direct replacements. This rule ignores patterns with the i (case-insensitive) or m (multiline) flags.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.