RegExp | NN 4 IE J3 ECMA n/a |
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 $. |
input | NN 4 IE J3 ECMA n/a |
Read/Write | |
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). | |
ExampleRegExp.input = "Four score and seven years ago..." | |
Value String. |
lastMatch | NN 4 IE J3 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 $&. | |
Examplevar matched = RegExp.lastMatch | |
Value String. |
lastParen | NN 4 IE J3 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 $+. | |
Examplevar myValue = RegExp.lastParen | |
Value String. |
leftContext, rightContext | NN 4 IE J3 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. | |
Examplevar wholeContext = RegExp.leftContext + RegExp.lastMatch + RegExp.rightContext | |
Value String. |
multiline | NN 4 IE J3 ECMA n/a |
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 $*. | |
Exampleif (RegExp.multiline) { ... } | |
Value Boolean. |
$1, ..., $9 | NN 4 IE J3 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). | |
ExampleRegExp.$2 | |
Value String. |