RegExp.$1-$9

非标准: 该特性是非标准的,请尽量不要在生产环境中使用它!

非标准 $1, $2, $3, $4, $5, $6, $7, $8, $9 属性是包含括号子串匹配的正则表达式的静态和只读属性。

语法

RegExp.$1
RegExp.$2
RegExp.$3
RegExp.$4
RegExp.$5
RegExp.$6
RegExp.$7
RegExp.$8
RegExp.$9

描述

$1, ..., $9 属性是静态的,他不是独立的正则表达式属性。所以,我们总是像这样子使用他们RegExp.$1, ..., RegExp.$9.

属性的值是只读的而且只有在正确匹配的情况下才会改变。

括号匹配项是无限的,但是 RegExp 对象能捕获的只有九个。你可以通过返回一个数组索引来取得所有的括号匹配项。

这些属性可以在String.replace 方法中替换字符串。在这种情况下,不用在前面加上 RegExp。下面的例子将详细说明。当正则表达式中不包含括号,脚本中的 $n's 就是字面上的意思 (当 n 是正整数).

示例

$nString.replace 中的应用

以下脚本用 replace() 方法去匹配一个 first last 格式的 nameString 实例 输出 last first 格式。在替换文本里,脚本用 $1$2 表示正则表达式中的括号匹配项的结果。

js
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
str.replace(re, "$2, $1"); // "Smith, John"
RegExp.$1; // "John"
RegExp.$2; // "Smith"

技术指标

非标准。不属于当前的任何规范。

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
RegExp.$1-$9
Deprecated

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Deprecated. Not for use in new websites.

参见