$1...$9 属性 (RegExp)

返回在模式匹配期间找到的,所存储的最近的九个部分。只读。

语法

RegExp.$n

参数

RegExp

始终为全局RegExp对象。

n

Any integer from 1 through 9.

备注

每当产生一个带括号的成功匹配时,$1...$9属性的值就被修改。可以在一个正则表达式模式中指定任意多个带括号的子匹配,但只能存储最新的九个。

示例

下面的示例执行正则表达式搜索。它显示了全局RegExp对象中的匹配项和子匹配项。子匹配项是$1...$9属性中包含的成功的带括号匹配项。该示例还显示了由exec方法返回的数组中的匹配项和子匹配项。

var newLine = "<br />";
var re = /(\w+)@(\w+)\.(\w+)/g
var src = "Please send mail to george@contoso.com and someone@example.com. Thanks!"
var result;
var s = "";
// Get the first match.
result = re.exec(src);
while (result != null) {
  // Show the entire match.
  s += newLine;
  // Show the match and submatches from the RegExp global object.
  s += "RegExp.lastMatch: " + RegExp.lastMatch + newLine;
  s += "RegExp.$1: " + RegExp.$1 + newLine;
  s += "RegExp.$2: " + RegExp.$2 + newLine;
  s += "RegExp.$3: " + RegExp.$3 + newLine;
  // Show the match and submatches from the array that is returned
  // by the exec method.
  for (var index = 0; index < result.length; index++) {
    s += index + ": ";
    s += result[index];
    s += newLine;
  }
  // Get the next match.
  result = re.exec(src);
}
document.write(s);
// Output:
// RegExp.lastMatch: george@contoso.com
// RegExp.$1: george
// RegExp.$2: contoso
// RegExp.$3: com
// 0: george@contoso.com
// 1: george
// 2: contoso
// 3: com
// RegExp.lastMatch: someone@example.com
// RegExp.$1: someone
// RegExp.$2: example
// RegExp.$3: com
// 0: someone@example.com
// 1: someone
// 2: example
// 3: com

必备条件

在以下文档模式中受支持:怪异模式、Internet Explorer 6标准模式、Internet Explorer 7标准模式、Internet Explorer 8标准模式、Internet Explorer 9标准模式、Internet Explorer 10标准模式、Internet Explorer 11标准模式。应用商店应用(Windows 8和Windows Phone 8.1)中也受支持。请参阅版本信息

Applies ToRegExp Object

如果你喜欢这篇文章,敬请给站长打赏↑

除特别注明外,本站所有文章均为本站站长原译,转载请注明出处。