JavaScript tutorial:
$1...$9 properties

 

Applies To: RegExp Object

The $1...$9 properties returns the nine most-recently memorized portions found during pattern matching. Read-only.

Syntax

RegExp.$n

The n argument is a number between 1 and 9.

Return value

Returns the nine most-recently memorized portions found during pattern matching

Remarks

The value of the $1...$9 properties is modified whenever a successful parenthesized match is made. Any number of parenthesized substrings may be specified in a regular expression pattern, but only the nine most recent can be stored.

The following example illustrates the use of the $1...$9 properties:

function matchDemo()
{
    var s;
    var re = new RegExp("d(b+)(d)","ig");
    var str = "cdbBdbsbdbdz";
    var arr = re.exec(str);
    s = "$1 contains: " + RegExp.$1 + "<BR>";
    s += "$2 contains: " + RegExp.$2 + "<BR>";
    s += "$3 contains: " + RegExp.$3;
    return(s);
}
document.write(matchDemo());

To run the code, paste it into JavaScript Editor, and click the Execute button.

See Also: RegExp Object Properties, Regular Expression Syntax