spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column5


JavaScript Regular Expressions

Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management

Backreferences

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

  • exec()
  • match()
  • test()
  • search()
  • replace()
  • split()

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 <-- RegExp.$1
www.webreference.com <-- RegExp.$2
 <-- RegExp.$3
/js/index.html <-- RegExp.$4

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:

 <-- RegExp.input
http://www.webreference.com/js/index.html <-- RegExp.lastMatch
/js/index.html <-- RegExp.lastParen
START  <-- RegExp.leftContext
false <-- RegExp.multiline
 END <-- RegExp.rightContext


Produced by Tomer Shiran and

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags



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