Backreferences in JavaScript regular expressions
- Published at
- Updated at
- Reading time
- 3min
Today I was preparing a slide deck about new features in JavaScript regular expressions and came across the article "Named capture groups" written by Axel Rauschmayer. The section about backreferences caught my eye.
There might be the situation that you're dealing with a regular expression that includes repeated character sequences like the following one: /(abc)(abc)(abc)/
. Instead of copying the character groups several times, pattern reuse is a better approach here. It turns out you can do that in JavaScript regular expressions.
When you define your regular expressions, you can reuse and backreference previous groups via \1
, \2
, etc..
/(๐)(๐ฏ)\1\2/.exec('๐๐ฏ๐๐ฏ');
// (3) ["๐๐ฏ๐๐ฏ", "๐", "๐ฏ", index: 0, input: "๐๐ฏ๐๐ฏ", ... ]
// Match:
// - a pizza
// - a burrito
// - a pizza (backreferenced)
// - a burrito (backreferenced)
/(๐)(๐ฏ)\1\2/.exec('๐๐ฏ๐');
// null (because one burrito is missing)
You can do the same for named capture groups via \k<name>
.
/(?<one>๐)(?<two>๐ฏ)\k<one>\k<two>/.exec('๐๐ฏ๐๐ฏ');
// (3) ["๐๐ฏ๐๐ฏ", "๐", "๐ฏ", index: 0, input: "๐๐ฏ๐๐ฏ", groups: {โฆ}]
// Match:
// - a pizza
// - a burrito
// - a pizza (backreferenced via the named capture group 'one')
// - a burrito (backreferenced via the named capture group 'two')
/(?<one>๐)(?<two>๐ฏ)\k<one>\k<two>/.exec('๐๐ฏ๐');
// null (because one burrito is missing)
Arnd Issler pointed out, that you can not talk about backreferences in regular expression without mentioning the references when using String
. So, here we go. ๐
Replacement references for capture groups
You can reference included capture groups using $1
, $2
, etc. in the replacement pattern.
MDN provides a good example to swap words using references.
const re = /(\w+)\s(\w+)/;
const str = 'Jane Smith';
const newstr = str.replace(re, '$2, $1');
console.log(newstr); // Smith, Jane
To follow the earlier examples you can have a look at the following "pizza-burrito-snippet":
'๐๐ฏ๐๐ฏ๐๐ฏ'.replace(
/(๐)(๐ฏ)\1/,
'first group: $1, second group: $2, rest:'
);
// "first group: ๐, second group: ๐ฏ, rest:๐ฏ๐๐ฏ"
As sequences such as $1
and $2
reference capture groups one might ask how to replace something with $1
without referencing an included capture group. In that case, you can use e.g. $$1
.
'๐๐ฏ๐๐ฏ๐๐ฏ'.replace(
/(๐)(๐ฏ)\1/,
'$$1 $$1 $$1 โ '
);
// "$1 $1 $1 โ ๐ฏ๐๐ฏ"
Replacement references for named capture groups
The same reference functionality works for named capture groups using $<name>
:
'๐๐ฏ๐๐ฏ๐๐ฏ'.replace(
/(?<one>๐)(?<two>๐ฏ)\k<one>/,
'first group: $<one>, second group: $<two>, rest:'
);
// "first group: ๐, second group: ๐ฏ, rest:๐ฏ๐๐ฏ"
If you want to replace something with $<name>
if there is a named capture group present you can use $$<name>
;
'๐๐ฏ๐๐ฏ๐๐ฏ'.replace(
/(?<one>๐)(?<two>๐ฏ)\k<one>/,
'$$<one> $$<one> $$<one> โ '
);
// "$<one> $<one> $<one> โ ๐ฏ๐๐ฏ"
I love these things โ if you do, too, you should definitely have a look at other replacement patterns of String
. This method provides more magic than you might think.
Speaking of browser support; you have to be careful. The support for named capture groups is still not great. Babel has you covered though. ๐
Ant that's it for today, see you next time. ๐
Yes? Cool! You might want to check out Web Weekly for more quick learnings. The last edition went out 15 days ago.