Backreferences (JavaScript)

Backreferences are used to find repeating groups of characters. They are also used to reformat an input string by rearranging the order and placement of the elements in the input string.

You can refer to a subexpression from within a regular expression, and from within a replacement string. Each subexpression is identified by number, and is referred to as a backreference.

Parentheses in a regular expression are used to create a subexpression. The resulting submatch can be retrieved by the program. For more information, see Alternation and Subexpressions (JavaScript).

Stored Submatches

You can refer to a subexpression from within a regular expression.

In a regular expression, each saved submatch is stored as it is encountered from left to right. The buffer numbers in which the submatches are stored begin at 1 and continue up to a maximum of 99 subexpressions. Within the regular expression, you can access each buffer by using \n, where n is one or two decimal digits identifying a specific buffer.

One application of backreferences provides the ability to locate the occurrence of two identical words together in a text. Take the following sentence: Is is the cost of of gasoline going up up?

This sentence contains several duplicated words. It would be useful to devise a way to fix the sentence without having to look for duplicates of every word. The following JavaScript regular expression uses a single subexpression to do that.

/\b([a-z]+) \1\b/gi

The subexpression in this case is everything enclosed in parentheses. That subexpression includes one or more alphabetical characters, as specified by [a-z]+. The second part of the regular expression is the reference to the previously saved submatch, that is, the second occurrence of the word just matched by the parenthetical expression. \1 is used to specify the first submatch.

The \b word boundary metacharacters make sure that only separate words are detected. Otherwise, a phrase such as "is issued" or "this is" would be incorrectly identified by this expression.

The following example lists the duplicated words. It shows how matches and submatches can be retrieved in code.

var newLine = "<br />";
var result;
var s = "";

var re = /\b([a-z]+) \1\b/gi
var src = "Is is the cost of of gasoline going up up?"

// Get the first match.
result = re.exec(src);

while (result != null) {
    // Show the entire match.
    s += newLine + result[0] + newLine;

    // Show the submatches.
    // You can also obtain the submatches from RegExp.$1,
    // RegExp.$2, and so on.
    for (var index = 1; index < result.length; index++) {
        s += "submatch " + index + ": ";
        s += result[index];
        s += newLine;
    }

    // Get the next match.
    result = re.exec(src);
}
document.write(s);

// Output:
//  Is is
//  submatch 1: Is

//  of of
//  submatch 1: of

//  up up
//  submatch 1: up

You can also refer to a subexpression from within a replacement string.

Using the regular expression shown above, the following example replaces an occurrence of two consecutive identical words with a single occurrence of the same word. In the replace method, $1 refers to the first saved submatch. If there is more than one submatch, you refer to them consecutively as $2, $3, and so on.

var re = /\b([a-z]+) \1\b/gi
var src = "Is is the cost of of gasoline going up up?"
var result = src.replace(re, "$1");
document.write (result);
// Output:
//  Is the cost of gasoline going up?

The following example exchanges each pair of words in the string.

var re = /(\S+)(\s+)(\S+)/gi
var src = "The quick brown fox jumps over the lazy dog."
var result = src.replace(re, "$3$2$1");
document.write (result);
// Output:
//  quick The fox brown over jumps lazy the dog.

See Also

Reference

replace Method (JavaScript)

Concepts

Alternation and Subexpressions (JavaScript)

Regular Expression Syntax (JavaScript)