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
const hasPrefix: boolean
hasPrefix
= /^foo/.
RegExp.test(string: string): boolean

Returns a Boolean value that indicates whether or not a pattern exists in a searched string.

@paramstring String on which to perform the search.

test
(
const str: any
str
);
const
const hasSuffix: boolean
hasSuffix
= /bar$/.
RegExp.test(string: string): boolean

Returns a Boolean value that indicates whether or not a pattern exists in a searched string.

@paramstring String on which to perform the search.

test
(
const str: any
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 ❤️‍🔥 around the world by the Flint team and contributors.