JavaScript Reference ยท RegExp
Regular expression backreferences
Reference for JavaScript regular expression backreferences: match the same text again with \1 or \k<name>, with repeated-word and matching-quote examples.
A backreference matches the same text that an earlier capture group matched. \1 refers to the first group, \2 to the second, and so on. With a named group (?<q>...) you write \k<q>.
This is different from repeating the group's pattern. (\d)\1 matches "77" but not "78", while (\d)\d would match both. Backreferences let a pattern say "the same thing again", which is how you find repeated words or a closing quote that matches its opening quote.
/(\w+) \1/ // a word followed by the same word /(["']).*?\1/ // text in matching quotes /(?<ch>.)\k<ch>/ // the same character twice, by name
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
\N // N is a number: the text matched by capturing group N \k<name> // the text matched by the named group (?<name>...)
| Part | Description |
|---|---|
\1 to \9, \10... |
The text captured by that numbered group. Groups are numbered by their opening parenthesis, from the left. |
\k<name> |
The text captured by the named group name. The name must exist in the pattern. |
Usage notes
- It matches text, not a pattern. Whatever the group matched at that moment is what the backreference must match exactly. The
iflag makes the comparison case-insensitive. - Groups that did not take part match the empty string. In
/(a)?b\1/, the string"b"matches because the unset group\1matches nothing. This is unlike many other regex engines. - A reference to a group that comes later (or contains the reference) also matches empty. Forward references are legal but rarely useful.
- Non-capturing groups are not numbered.
(?:...)does not shift the numbers, so use it freely for grouping. Counting parentheses is error-prone in long patterns, which is the best reason to prefer named groups. - Inside a pattern use
\1; in a replacement string use$1. Inreplace()the replacement text is a plain string where$1,$<name>and$&are special.\1there is just a backslash and a digit. See String.prototype.replace(). \k<name>is only a backreference when the pattern has named groups (or theuorvflag is on). In a legacy pattern without named groups,\kis a literalk. Always use theuflag in new code to avoid these legacy readings.\10ambiguity. If the pattern has fewer than ten groups, a legacy-mode\10can be read as an octal escape. With theuflag it is a syntax error instead of a surprise.- Backtracking cost. Patterns with backreferences cannot be matched by the fastest linear-time algorithms. Do not use them on untrusted, very long inputs without a length limit.
- Duplicate group names in different alternatives (
(?<y>\d{4})-|-(?<y>\d{4})) were added to the language recently; check a compatibility table before using them.
Browser support
Numbered backreferences work everywhere. Named groups and \k<name> are supported by all current browsers.
Related
- Capture groups โ numbered and named groups
- String.prototype.replace() โ
$1in replacements - Regular expression flags
- Lesson: Regular Expressions
- Lesson: Regex in Practice