Backreferences in pattern: \N and \k<name>

The contents of capturing groups can be used not only in the replacement string or in the result, but also the pattern.

In this chapter, we will explore backreference in the pattern such as \N and \k<name>.

Backreference by number: \N

In the pattern, a group can be referenced by using \N ( it is the group number). To make it more precise, let’s consider a case.

Imagine, you want to find quoted strings. Both single-quoted and double-quoted variants should match. Now, let’s see how to find them.

Put both types of quotes into square brackets ['"](.*?)['"] is possible, but the strings with mixed quotes such as "...' and '..." will be found. It may lead to incorrect matches as in the string “He is the one!”:

Javascript regexp backreference by number
let str = `SHe said: "He's her brother!".`; let regexp = /['"](.*?)['"]/g; // The result is not what you want to see console.log(str.match(regexp)); // "He'

So, the pattern found an opening quote, then the text is consumed until the other quote '.

Let’s see the correct code:

Javascript regexp backreference by number
let str = `She said: "He's her brother!".`; let regexp = /(['"])(.*?)\1/g; console.log(str.match(regexp)); // "He's her brother!"

Now, it works correctly. The engine of the regexp detects the initial quote (['"])and remembers its content.

Further, \1 in the pattern considers “find the same text as in the initial group”. Like that, \2 means the contents of the second group, and so on.

An important thing to note: if you apply ?: in the group, it can’t be referenced. Excluded from capturing (?:...) groups can not be remembered by the engine.

Backreference by name: \k<name>

If there are many parentheses in a regexp, it is convenient to name them./p>

For referencing a named group, you can use \k<name>.

Let’s consider an example of a group with quotes.

So, the name of the group will be ?<quote>, and the backreference will be \k<quote>,, like this:

Javascript regexp backreference by name
let str = `SHe said: "He's her brother!".`; let regexp = /(?<quote>['"])(.*?)\k<quote>/g; console.log(str.match(regexp)); // "He's her brother!"

Practice Your Knowledge

What can be used as backreferences in JavaScript regular expressions?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?