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.
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:
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.
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">This displays "Smith, John". Example 2. : In the following example,
re = /(\w+)\s(\w+)/;
str = "John Smith";
newstr=str.replace(re, "$2, $1");
document.write(newstr)
</SCRIPT>
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>
Last Updated: 10/22/97 11:48:14