spacer

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

home / experts / javascript / column5


JavaScript Regular Expressions

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

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

http://www.internet.com

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

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
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
Crucial Triples Up With New Three-Channel DDR3 Kits · Meet the Finalists: Excellence in Technology Awards · Tealeaf Offers Insight to Mobile Customer Behavior

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