Skip to content

regexNamedBackreferences

Reports backreferences that do not use the name of their referenced capturing group.

✅ This rule is included in the ts stylisticStrict presets.

When a capturing group has a name (e.g., (?<name>...)), backreferences to that group should use the named syntax \k<name> instead of the numeric syntax \1. Named backreferences are more readable and maintainable, especially when regex patterns change over time.

This rule reports backreferences that do not use the name of their referenced capturing group.

const pattern = /(?<word>\w+)\s+\1/;
const pattern = /(?<first>a)(?<second>b)\1\2/;

This rule is not configurable.

If you prefer the brevity of numeric backreferences, or if your codebase has established conventions around using numeric backreferences even for named groups, you might prefer to disable this rule. Some teams find numeric backreferences more familiar from other regex dialects.

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