A substring matched by a grouping can also be referenced within the regular expression, which is known as backreferencing. Backreferencing allows you to make matches later in a regular expression depending on what matched earlier in the regular expression. You can reference a previous grouping with \x, where x is the grouping position.
The following table provides some backreferencing examples:
| Regular Expression | Matching String |
|---|---|
/(~np~\~/np~w+) ~np~\~/np~1/ |
The same word repeated twice. For example, the echo ha ha… |
/(~np~\~/np~w+)~np~\~/np~1/ |
Words with repeated parts. For example, mama, papa, coco… |
/(~np~\~/np~d)(~np~\~/np~d)(~np~\~/np~d)~np~\~/np~2~np~\~/np~1 |
Any five-digit palindrome number. For example, 12321, 83638, 91119… |