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
110
111
112
113
114
115
116
117
118
119
120
|
---
title: 'RegExp.prototype[@@replace]()'
slug: Web/JavaScript/Reference/Global_Objects/RegExp/@@replace
tags:
- JavaScript
- RegExp
- 正则表达式
translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/@@replace
---
<div>{{JSRef}}</div>
<p> <strong><code>[@@replace]()</code></strong> 方法会在一个字符串中用给定的替换器,替换所有符合正则模式的匹配项,并返回替换后的新字符串结果。用来替换的参数可以是一个字符串或是一个针对每次匹配的回调函数。</p>
<p>{{EmbedInteractiveExample("pages/js/regexp-prototype-@@replace.html")}}</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><var>regexp</var>[Symbol.replace](str, <var>newSubStr</var>|<var>function</var>)</pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>str</code></dt>
<dd>正则替换的目标字符串。</dd>
<dt><code>newSubStr (replacement)</code></dt>
<dd>类型为 {{jsxref("String")}} 的替换器。支持大多数特殊的替换匹配模式; 见{{jsxref("String.prototype.replace()")}}页的{{jsxref("String.prototype.replace", "Specifying a string as a parameter", "#Specifying_a_string_as_a_parameter", 1)}}部分。</dd>
<dt><code>function (replacement)</code></dt>
<dd>生成新的子字符串的回调函数替换器。作用于该函数的参数的详细描述见{{jsxref("String.prototype.replace()")}}页的 {{jsxref("String.prototype.replace", "Specifying a function as a parameter", "#Specifying_a_function_as_a_parameter", 1)}} 部分。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>用替换器替换相应匹配项后的新字符串。</p>
<h2 id="描述">描述</h2>
<p>如果匹配模式也是{{jsxref("RegExp")}}对象,这个方法在 {{jsxref("String.prototype.replace()")}} 的内部调用。例如,下面的两个方法返回相同结果。</p>
<pre class="brush: js">'abc'.replace(/a/, 'A');
/a/[Symbol.replace]('abc', 'A');</pre>
<p>该方法是为了在RegExp子类中自定义匹配的替换模式。</p>
<p>如果匹配模式不是一个{{jsxref("RegExp")}} 对象, {{jsxref("String.prototype.replace()")}} 就不会调用该方法,也不会创建一个 {{jsxref("RegExp")}}对象。</p>
<h2 id="示例">示例</h2>
<h3 id="直接调用">直接调用</h3>
<p>这个方法基本可以和 {{jsxref("String.prototype.replace()")}} 一样使用, 不同之处是 <code>this</code> 和参数顺序。</p>
<pre class="brush: js">var re = /-/g;
var str = '2016-01-01';
var newstr = re[Symbol.replace](str, '.');
console.log(newstr); // 2016.01.01
</pre>
<h3 id="在子类中使用replace">在子类中使用@@replace</h3>
<p>{{jsxref("RegExp")}} 的子类可以覆写 <code>[@@replace]()</code>方法来修改默认行为。</p>
<pre class="brush: js">class MyRegExp extends RegExp {
constructor(pattern, flags, count) {
super(pattern, flags);
this.count = count;
}
[Symbol.replace](str, replacement) {
// Perform @@replace |count| times.
var result = str;
for (var i = 0; i < this.count; i++) {
result = RegExp.prototype[Symbol.replace].call(this, result, replacement);
}
return result;
}
}
var re = new MyRegExp('\\d', '', 3);
var str = '01234567';
var newstr = str.replace(re, '#'); // String.prototype.replace calls re[@@replace].
console.log(newstr); // ###34567</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
<th scope="col">备注</th>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-regexp.prototype-@@replace', 'RegExp.prototype[@@replace]')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>初始定义</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-regexp.prototype-@@replace', 'RegExp.prototype[@@replace]')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div>
<p>{{Compat("javascript.builtins.RegExp.@@replace")}}</p>
</div>
<h2 id="另见">另见</h2>
<ul>
<li>{{jsxref("String.prototype.replace()")}}</li>
<li>{{jsxref("RegExp.prototype.@@match()", "RegExp.prototype[@@match]()")}}</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>
|