Regular Expressions: Backreferences - Doc JavaScript | WebReference

Regular Expressions: Backreferences - Doc JavaScript


JavaScript Regular Expressions

Backreferences

When you invoke any of the following methods, and a match is found, the global RegExp object is updated:

In fact, this global object features several useful properties, which reflect specific attributes of the most recent successful match. The following table lists all of these properties, along with a short description for each. Note that some of the properties have both long and short (Perl-like) names. Perl is the programming language from which JavaScript modeled its regular expressions.

PropertyDescription
RegExp.$1
RegExp.$2
RegExp.$3
RegExp.$4
RegExp.$5
RegExp.$6
RegExp.$7
RegExp.$8
RegExp.$9
Read-only properties that contain the text matched by the corresponding set of parentheses in the last pattern matched. The number of possible parenthesized substrings is unlimited, but the RegExp object can only hold the last nine.
RegExp.indexA read-only property that indicates where the first successful match begins in a string that was searched. This property is not supported by Navigator 4.0x.
RegExp.lastIndexA read-only property that indicates where the last successful match begins in a string that was searched. This property is not supported by Navigator 4.0x.
RegExp.input
RegExp["$_"]
A read-only property that contains the string against which a search was performed. In Navigator, you can also assign a value to this property. See Netscape's documentation for more Navigator-specific details on this property.
RegExp.lastMatch
RegExp["$&"]
A read-only property that holds the substring matched by the last successful pattern match.
RegExp.lastParen
RegExp["$+"]
A read-only property that specifies the last parenthesized substring match, if any.
RegExp.leftContext
RegExp["$`"]
A read-only property that specifies the string preceding whatever was matched by the last successful pattern match.
RegExp.multiline
RegExp["$*"]
A read-only Boolean property that reflects whether or not to search strings across multiple lines. In Navigator, you can also assign a value to this property. The use of this variable in Perl is now deprecated, because the /m modifier is supported. This property only influences the interpretation of ^ and $ in a regular expression.
RegExp.rightContext
RegExp["$'"]
A read-only property that specifies the string following whatever was matched by the last successful pattern match.

Most of the Perl-like properties are buggy or do not work. You should avoid the short notation. Furthermore, Internet Explorer 4.0 only supports the input property (and $1, ..., $9). Be extremely cautious when writing scripts that depend on these properties.

That sums up all the properties of the RegExp object, under Navigator 4.0x and Internet Explorer 4.0. Some of these properties might not be crystal clear yet, so let's take a look at an example:

var str = "START http://www.webreference.com/js/index.html END";
var ar = str.match(/(\w+)://([^/:]+)(:\d*)?([^# ]*)/);

We'll use a few scripts to find the value of each of the properties. The first one:

document.write(RegExp.$1, "<BR>",
               RegExp.$2, "<BR>",
               RegExp.$3, "<BR>",
               RegExp.$4);

produces the following output (notice that one of the properties is not defined, so a blank line is created):

http 
www.webreference.com 
 
/js/index.html 

We'll use the following script to find the values of the rest of the properties:

document.write(RegExp.input, "<BR>",
               RegExp.lastMatch, "<BR>",
               RegExp.lastParen, "<BR>",
               RegExp.leftContext, "<BR>",
               RegExp.multiline, "<BR>",
               RegExp.rightContext);

This statement produces the following output under Navigator 4.0x:

 
http://www.webreference.com/js/index.html 
/js/index.html 
START  
false 
 END 

http://www.internet.com

Created: October 23, 1997, 1997
Revised: December 4, 1997
URL: http://www.webreference.com/js/column5/backreferences.html