[Contents]
[Previous] [Next] [Last]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 predefinedRegExp 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."
RegExp.propertyName
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.
None.
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.
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:
TEXT form element, input
is set to the value of the contained text. TEXTAREA form element, input
is set to the value of the contained text. Note that multiline is also set to
true so that the match can be executed over the multiple lines of text. SELECT form element, input
is set to the value of the selected text. Link object, input is
set to the value of the text between <A HREF=...> and </A>.
The value of the input property is cleared after the event handler
completes.
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 thereplace
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