Regex Backreference
A feature that reuses text matched by a capture group within the same pattern. Referenced using \1, \2, etc.
A regex backreference is a feature that reuses text matched by a capture group () within the same regular expression pattern using \1, \2 notation.
For example, (\w+)\s+\1 matches "consecutive identical words." HTML tag matching <(\w+)>.*?</\1> is another classic backreference use case. Regular expressions mastery books cover advanced patterns.
In replacement operations, $1, $2 (or \1, \2 depending on the language) reference capture group contents. This is a powerful tool for text formatting and transformation.
For character counting, backreference-based regex is useful for detecting repeated patterns, enabling duplicate string detection and text redundancy analysis. Regex practical guides provide additional context.