[Previous] [Next] [TOC] 

The RegExp Object

Core object. A global object with properties that are set either before a search for a regular expression in a string, or after a match is found. (This is not to be confused with the regular expression object which contains the regular expression pattern.)




propertyName is one of the properties listed below.


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.

A read/write property that reflects the string against which the regular expression is matched. This value is preset as described in the Description below. If no string argument is provided to the regular expression's exec or test methods, and if RegExp.input has a value, its value is used as the argument. 
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.
A read-only property that specifies the last matched characters.
A read-only property that specifies the last parenthesized substring match, if any.
A read-only property that specifies the string up to the most recent match.
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 String replace method. 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 $#'s literally (where # is a positive integer).




The RegExp global 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 String methods, match, and replace.

The script or the Navigator 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. input is set by the Navigator in the following cases:

input is cleared after each of the above calls.

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


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.

re = /(\w+)\s(\w+)/;
str = "John Smith";
newstr=str.replace(re, "$2, $1");

This prints "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.

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

[Previous] [Next] [TOC] 

Copyright © 1997 Netscape Communications Corporation