The legacy RegExp $1, $2, $3, $4, $5, $6, $7, $8, $9 properties are static and read-only properties of regular expressions that contain parenthesized substring matches.
The $1, ..., $9 properties are static, they are not a property of an individual regular expression object. Instead, you always use them as
RegExp.$1
, ...,
RegExp.$9
.
The values of these properties are read-only and modified whenever successful matches are made.
The number of possible parenthesized substrings is unlimited, but the
RegExp
object can only hold the first nine. You can access all parenthesized substrings through the returned array's indexes.
These properties can be used in the replacement text for the
String.replace
method. When used this way, do not prepend them with
RegExp
. The example below illustrates this. When parentheses are not included in the regular expression, the script interprets
$n
's literally (where
n
is a positive integer).
The following script uses the
replace()
方法在
String
instance to match a name in the format
first last
and output it in the format
last, first
. In the replacement text, the script uses
$1
and
$2
to indicate the results of the corresponding matching parentheses in the regular expression pattern.
var re = /(\w+)\s(\w+)/; var str = 'John Smith'; str.replace(re, '$2, $1'); // "Smith, John" RegExp.$1; // "John" RegExp.$2; // "Smith"
The following script uses the
test()
方法在
RegExp
instance to grab a number in a generic string.
var str = 'Test 24'; var number = /(\d+)/.test(str) ? RegExp.$1 : '0'; number; // "24"
Please note that any operation involving the usage of other regular expressions between a
re.test(str)
call and the
RegExp.$n
property, might have side effects, so that accessing these special properties should be done instantly, otherwise the result might be unexpected.
| 规范 |
|---|
| Legacy RegExp features in JavaScript |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
RegExp.$1-$9
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 5 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
完整支持
RegExp.input ($_)
RegExp.lastMatch ($&)
RegExp.lastParen ($+)
RegExp.leftContext ($`)
RegExp.rightContext ($')
RegExp
RegExp.$1-$9
RegExp.input ($_)
RegExp.lastMatch ($&)
RegExp.lastParen ($+)
RegExp.leftContext ($`)
RegExp.prototype.dotAll
RegExp.prototype.flags
RegExp.prototype.global
RegExp.prototype.ignoreCase
RegExp.prototype.multiline
RegExp.prototype.source
RegExp.prototype.sticky
RegExp.prototype.unicode
RegExp.rightContext ($')
RegExpInstance.lastIndex
get RegExp[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()