aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/regexp/@@match/index.html
blob: 28b4431659b161ea40102f5f7f75b6cc29d639e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
title: 'RegExp.prototype[@@match]()'
slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@match
tags:
  - JavaScript
  - RegExp
  - 正则
translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@match
---
<div>{{JSRef}}</div>

<p><em>正则表达式</em>匹配<em>字符串</em>时,<strong><code>[@@match]()</code></strong>方法用于获取匹配结果。</p>

<p>{{EmbedInteractiveExample("pages/js/regexp-prototype-@@match.html")}}</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><var>regexp</var>[Symbol.match](str)</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>str</code></dt>
 <dd>match 的目标参数是{{jsxref("String")}}</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>match 方法会返回一个数组,它包括整个匹配结果,和通过捕获组匹配到的结果,如果没有匹配到则返回null</p>

<h2 id="描述">描述</h2>

<p>这个方法在 {{jsxref("String.prototype.match()")}} 的内部调用。例如,下面的两个方法返回相同结果。</p>

<pre class="brush: js">'abc'.match(/a/);

/a/[Symbol.match]('abc');</pre>

<p>这个方法为自定义 <code>RegExp</code> 子类中的匹配行为而存在。</p>

<h2 id="示例">示例</h2>

<h3 id="直接调用">直接调用</h3>

<p>这个方法的使用方式和 {{jsxref("String.prototype.match()")}} 相同,不同之处是 <code>this</code> 和参数顺序。</p>

<pre class="brush: js">var re = /[0-9]+/g;
var str = '2016-01-02';
var result = re[Symbol.match](str);
console.log(result);  // ["2016", "01", "02"]
</pre>

<h3 id="在子类中使用match">在子类中使用<code>@@match</code></h3>

<p>{{jsxref("RegExp")}} 的子类可以覆写 <code>[@@match]()</code>方法来修改默认行为。</p>

<pre class="brush: js">class MyRegExp extends RegExp {
  [Symbol.match](str) {
    var result = RegExp.prototype[Symbol.match].call(this, str);
    if (!result) return null;
    return {
      group(n) {
        return result[n];
      }
    };
  }
}

var re = new MyRegExp('([0-9]+)-([0-9]+)-([0-9]+)');
var str = '2016-01-02';
var result = str.match(re); // String.prototype.match calls re[@@match].
console.log(result.group(1)); // 2016
console.log(result.group(2)); // 01
console.log(result.group(3)); // 02
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@match', 'RegExp.prototype[@@match]')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>
<p>{{Compat("javascript.builtins.RegExp.@@match")}}</p>
</div>

<h2 id="另见">另见</h2>

<ul>
 <li>{{jsxref("String.prototype.match()")}}</li>
 <li>{{jsxref("RegExp.prototype.@@replace()", "RegExp.prototype[@@replace]()")}}</li>
 <li>{{jsxref("RegExp.prototype.@@search()", "RegExp.prototype[@@search]()")}}</li>
 <li>{{jsxref("RegExp.prototype.@@split()", "RegExp.prototype[@@split]()")}}</li>
 <li>{{jsxref("RegExp.prototype.exec()")}}</li>
 <li>{{jsxref("RegExp.prototype.test()")}}</li>
</ul>