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.
Examples
Section titled “Examples”const pattern = /(?<word>\w+)\s+\1/;const pattern = /(?<first>a)(?<second>b)\1\2/;const pattern = /(?<word>\w+)\s+\k<word>/;const pattern = /(?<first>a)(?<second>b)\k<first>\k<second>/;// Unnamed groups can use numeric backreferencesconst pattern = /(a)\1/;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
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.