[Contents] [Previous] [Next] [Last]

Chapter 11
The RegExp Object

Core object. A predefined object with properties that are set either before a search for a regular expression in a string, or after a match is found. JavaScript updates the properties of this same object every time you work with any regular expression.

You use the predefined RegExp object in conjunction with individual regular expression objects you create that contain the regular expression pattern. Creating and using these regular expression objects is described in Chapter 12, "Regular Expression Objects."

Syntax

RegExp.propertyName

Parameters

propertyName is one of the properties listed below.

Properties

Note that six of the RegExp properties have both long and short (Perl-like) names. Both names always refer to the same value. Perl is the programming language from which JavaScript modeled its regular expressions.

input  
$_
A read/write property that reflects the string against which a regular expression is matched. This value is preset as described below. If no string argument is provided to a regular expression's exec or test methods, and if RegExp.input has a value, its value is used as the argument. 

multiline  
$*
A read/write Boolean property that reflects whether or not to search in strings across multiple lines; true if multiple lines are searched, false if searches must stop at line breaks.

lastMatch  
$&
A read-only property that specifies the last matched characters.

lastParen  
$+
A read-only property that specifies the last parenthesized substring match, if any.

leftContext  
$\Q
A read-only property that specifies the string up to the most recent match.

rightContext 
$'
A read-only property that specifies the string past the most recent match.

$1, ..., $9
Read-only properties that contain parenthesized substring matches, if any. The number of possible parenthesized substrings is unlimited, but the RegExp object can only hold the last nine. You can access all parenthesized substrings through the returned array's indexes. 

These properties can be used in the replacement text for the replace method of String. When used this way, do not prepend them with RegExp. Example 1 illustrates this. When parentheses are not included in the regular expression, the script interprets $n's literally (where n is a positive integer).

Methods

None.

Description

A separate predefined RegExp object is available in each window; that is, each separate thread of JavaScript execution gets its own RegExp object. Because each script runs to completion without interruption in a thread, this assures that different scripts do not overwrite values of the RegExp object.

The predefined RegExp object contains the properties listed above. Except for input and multiline whose values can be preset, property values are set after execution of the regular expression methods exec and test, and the match and replace methods of String.

The script or the browser can preset the input property. If preset and if no string argument is explicitly provided, input's value is used as the string argument to the exec or test methods of the regular expression object. input is set by the browser in the following cases:

The value of the input property is cleared after the event handler completes.

The script or the browser can preset the multiline property. When an event handler is called for a TEXTAREA form element, the browser sets multiline to true. multiline is cleared after the event handler completes. This means that, if you've preset multiline to true, it is reset to false after the execution of any event handler.

Examples

Example 1: The following script uses the replace method to switch the words in the string. For the replacement text, the script uses the values of the $1 and $2 properties of the global RegExp object. Note that the RegExp object name is not be prepended to the $ properties when they are passed as the second argument to the replace method.

<SCRIPT LANGUAGE="JavaScript1.2">
re = /(\w+)\s(\w+)/;
str = "John Smith";
newstr=str.replace(re, "$2, $1");
document.write(newstr)
</SCRIPT>
This displays "Smith, John".

Example 2. : In the following example, RegExp.input is set by the Change event. In the getInfo function, the exec method uses the value of RegExp.input as its argument. Note that RegExp is prepended to the $ properties.

<HTML>
<SCRIPT LANGUAGE="JavaScript1.2">
function getInfo() {
   re = /(\w+)\s(\d+)/;
   re.exec();
   window.alert(RegExp.$1 + ", your age is " + RegExp.$2);
}
</SCRIPT>
Enter your first name and your age, and then press Enter.
<FORM>
<INPUT TYPE:"TEXT" NAME="NameAge" onChange="getInfo(this);">
</FORM>
</HTML>


[Contents] [Previous] [Next] [Last]

Last Updated: 10/22/97 11:48:14


Copyright 1997 Netscape Communications Corporation