The RegExp constructor contains some properties. These properties apply to all regular expressions in the scope and vary based on the most recent regular expression operation performed. Another unique thing about these attributes is that they can be accessed in two ways. In other words, these attributes have a long property name, ha a short attribute name (opera is the exception, it does not support short property names). The following table lists the properties of the RegExp constructor.
Long Property name |
Short attribute name |
Description |
Input |
$_ |
The last string to match. Opera does not implement this property |
Lastmatch |
$& |
The last occurrence of the match. Opera to implement this property |
Lastparen |
$+ |
Most recently matched capture group |
Leftcontext |
$` |
Text before lastmatch in the input string |
Multiline |
$* |
Boolean value that indicates whether all expressions use multiline mode IE and opera do not implement this property |
Rightcontext |
$ |
Text after lastmatch in the input string |
Use these properties to extract more specific information from the operations performed by exec () or test (). Take a look at the following example:
var text = "This ha been a short summer"; var pattern =/(.) hort/g; if (pattern.test (text)) {alert (regexp.input);//"This has been a short summer" alert (regexp.leftcontext);//"This has bee n A "alert" (Regexp.rightcontext); "Summer" alert (Regexp.lastmatch); "Short" alert (Regexp.lastparen); "S" alert (regexp.multiline); False}
The code above creates a pattern that matches any one character followed by Hort, and puts the first character in a capturing group. The properties of the RegExp constructor return the following values:
- The input property returns the original string;
- The Leftcontext property returns the string before the word short, and the Rightcontext property returns the string after short;
- The Lastmatch property returns the most recent string that matches the entire regular expression, that is, the short
- The Lastparen property returns the most recent matching capturing group, which is the example s
As mentioned earlier, the long attribute names used by examples can be replaced with the corresponding short attribute names. However, because these short property names are mostly not valid ECMAScript identifiers, they must be accessed through the square brackets method, as follows:
var text = "This has been short summer"; var pattern =/(.) hort/g; if (pattern.test (text)) {alert (regexp.$_);//this has been a short summer alert (regexp["$ '"]);//this has been a alert (Re gexp["s '"]); Summer alert (regexp["$&"]); Short alert (regexp["$+"]); S alert (regexp["$*"]); False}
In addition to the several properties described above, there are up to 9 constructor properties that are used to store capturing groups. The syntax for accessing these properties is Regexp.$1, regexp.$2 ... Regexp.$9, respectively for storage first, second ... The Nineth matching capturing group. These properties are automatically populated when the exec () or test () method is invoked. Then we can use them as follows:
var text = "This has been a short summer"; var pattern =/(..) or (.) /g; if (pattern.test (text)) {alert (regexp.$1);//sh alert (regexp.$2);//t}
This creates a pattern that contains two capturing groups and tests a string with that pattern. Even though the test () method value returns a Boolean value, the properties of the RegExp constructor, and $, are automatically populated with strings that match the corresponding capturing group.