I have come across a piece of code in JScript:
RegExp.$1
Does anybody know what it does?
If I output it on its own, I get nothing not e
To add some details:
(As already said,) the RegExp.$n
-property (where n
is a single digit 1-9) returns the last n
th parenthesized (captured) substring in a match.
These properties were first implemented in JavaScript 1.2 and deprecated in JavaScript 1.5 - when RegExp
underwent a major change and many of the results from RegExp.prototype.exec(string)
were moved from the RegExp
object to the RegExp
instance and all the .$
properties (and their full name versions (except for .multiline
)) "went away".
The non-standard1 $1
, $2
, $3
, $4
, $5
, $6
, $7
, $8
, $9
properties are static and read-only properties of regular expressions (that contain parenthesized substring matches) and are modified whenever successful matches are made.
They are not a property of an individual regular expression object. Instead, you always use them as RegExp.$1
, ..., RegExp.$9
.
The number of possible parenthesized substrings is unlimited (of course), but the RegExp
object can only hold the last nine.
1 Non-standard = Not part of any current specification!
You can find the definition and references in the following sections of the ECMA-262 3 Specs:
String.prototype.match(regexp)
String.prototype.replace(regexp)
RegExp.prototype.exec(string)