About This Product
Search This Database:
| Over 5000 Free Fonts | Tutorials | Javascript Forum | Other Javascript Resources | Cheat Sheet
Tutorial archive
General Questions
Javascript 1.2
Object Model
Reference Manual
Mouse Events
File Access
Control Status Bar
Dates and Time
Client Information

[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."




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 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. 
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 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).




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.


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");

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.

<SCRIPT LANGUAGE="JavaScript1.2">
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);">

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