Skip to content

regexPredefinedAssertions

Reports regex lookarounds that can be replaced with predefined assertions.

✅ This rule is included in the ts stylistic presets.

Regular expressions support predefined assertions like \b (word boundary), \B (non-word boundary), ^ (start of string), and $ (end of string). These are more concise and communicate intent more directly than equivalent lookaround patterns.

This rule reports lookarounds that can be simplified:

  • (?=\w) and (?<=\w)\B (non-word boundary)
  • (?!\w) and (?<!\w)\b (word boundary)
  • (?<!.) at pattern start → ^ (start of string)
  • (?!.) at pattern end → $ (end of string)
const pattern = /a(?=\w)b/;
const pattern = /a(?!\w)b/;
const pattern = /(?<!.)abc/;
const pattern = /abc(?!.)/;

This rule is not configurable.

If you prefer the explicitness of lookaround assertions for documentation purposes, or if you need to maintain consistency with more complex patterns that require lookarounds, you might prefer to disable this rule.

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