stringStartsEndsWith
Reports regex patterns that can be replaced with
endsWithorstartsWith.
✅ 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.
Examples
Section titled “Examples”Prefix Check with ^
Section titled “Prefix Check with ^”const hasPrefix = /^foo/.test(str);const hasPrefix = str.startsWith("foo");Suffix Check with $
Section titled “Suffix Check with $”const hasSuffix = /bar$/.test(str);const hasSuffix = str.endsWith("bar");Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.