RegExp | NN 4 IE 4 ECMA 3 | |||||||||||||||||
The RegExp object is a static object that both generates instances of a regular expression and monitors all regular expression in the current window or frame. Instances of the RegExp object are covered in the regular expressions object description that follows this section. |
||||||||||||||||||
Regular expressions assist in locating text that matches patterns of characters or characteristics. For example, a regular expression can be used to find out very quickly if an entry in a text field is a five-digit number. Defining the pattern to match requires knowledge of a separate notation syntax that is beyond the scope of this book (but is covered in Mastering Regular Expressions, by Jeffrey E. F. Friedl, published by O'Reilly). A summary of the syntax can be found in the description of the regular expression object. |
||||||||||||||||||
Properties of the RegExp object store information about the last operation of any regular expression in the document. Therefore, it is conceivable that each property could change after each regular expression operation. Such operations include not only the methods of a regular expression object instance (exec( ) and test( )), but also the String object methods that accept regular expressions as parameters (match( ), replace( ), and split( )). Some of these properties are passed to the regular expression object as well, in preparation for the next operation with the regular expression. |
||||||||||||||||||
All properties have verbose names as well as shortcut names that begin with $. |
||||||||||||||||||
Properties | ||||||||||||||||||
|
index | NN n/a IE 4 ECMA n/a |
Read-only | |
This is the zero-based index value of the character position within the string where the most recent search for the pattern began. The lastIndex property provides the end position. |
|
Example | |
var srchStart = RegExp.index; |
|
Value | |
Integer. |
input | NN 4 IE 4 ECMA n/a |
Read/Write | |
This is the main string against which a regular expression is compared. If the main string is handed to the regular expression operation as a parameter to a method, this value is null. The short version is $_ (dollar sign, underscore). |
|
Example | |
RegExp.input = "Four score and seven years ago..."; |
|
Value | |
String. |
lastIndex | NN n/a IE 4 ECMA n/a |
Read/Write | |
This is the zero-based index value of the character within the string where the next search for the pattern begins. In a new search, the value is zero. You can also set the value manually if you wish to start at a different location or skip some characters. This property is echoed in the regular expression object instance, and is supported there in Navigator. |
|
Example | |
myRE.lastIndex = 30; |
|
Value | |
Integer. |
lastMatch | NN 4 IE 5(Mac)/5.5(Win) ECMA n/a |
Read-only | |
Returns the string that matches the regular expression as a result of the most recent operation. The short version is $&. |
|
Example | |
var matched = RegExp.lastMatch; |
|
Value | |
String. |
lastParen | NN 4 IE 5(Mac)/5.5(Win) ECMA n/a |
Read-only | |
Returns the string that matches the last parenthesized subcomponent of the regular expression as a result of the most recent operation. The short version is $+. |
|
Example | |
var myValue = RegExp.lastParen; |
|
Value | |
String. |
leftContext, rightContext | NN 4 IE 5(Mac)/5.5(Win) ECMA n/a |
Read-only | |
The leftContext property returns the string starting with the beginning of the most recent searched text up to, but not including, the matching string. The rightContext property returns the string starting with the main string portion immediately following the matching string and extending to the end of the string. The short versions are $` and $', respectively. Because the start of subsequent searches on the same main string move inexorably toward the end of the main string, the starting point of the leftContext value can shift with each operation. |
|
Example | |
var wholeContext = RegExp.leftContext + RegExp.lastMatch + RegExp.rightContext; |
|
Value | |
String. |
multiline | NN 4 IE 5(Mac)/5.5(Win) ECMA 3 |
Read/Write | |
If the search extends across multiple lines of text, the multiline property is set to true. A search through text in a textarea element, for example, is multiline. The short version is $*. |
|
Example | |
if (RegExp.multiline) { ... } |
|
Value | |
Boolean. |
prototype | NN 4 IE 4 ECMA 3 |
Read/Write | |
See this property for the Array object. |
$1, ..., $9 | NN 4 IE 4 ECMA n/a |
Read-only | |
Parenthesized subcomponents of a regular expression return results. These results are stored individually in properties labeled 1 through 9, preceded by the $ shortcut symbol. The order is based on the position of the left parenthesis of a subcomponent: the leftmost subcomponent result is placed into $1. These properties may be used directly within parameters to String methods that use regular expressions (see the String.replace( ) method). |
|
Example | |
RegExp.$2 |
|
Value | |
String. |