aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/replaceall/index.html
blob: c9d892e3f75fd430a4b9150a05feeffcf73ec921 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
---
title: String.prototype.replaceAll()
slug: Web/JavaScript/Reference/Global_Objects/String/replaceAll
tags:
  - global flag / g
  - replace
  - replaceAll
translation_of: Web/JavaScript/Reference/Global_Objects/String/replaceAll
---
<div>{{JSRef}}</div>

<p><strong><code>replaceAll()</code></strong> 方法返回一个新字符串,新字符串所有满足 <code>pattern</code> 的部分都已被<code>replacement</code> 替换。<code>pattern</code>可以是一个字符串或一个 {{jsxref("RegExp")}}<code>replacement</code>可以是一个字符串或一个在每次匹配被调用的函数。</p>

<p>原始字符串保持不变。</p>

<div>{{EmbedInteractiveExample("pages/js/string-replaceall.html")}}</div>



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

<pre class="syntaxbox">const newStr = <var>str</var>.replaceAll(<var>regexp</var>|<var>substr</var>, <var>newSubstr</var>|<var>function</var>)</pre>

<div class="blockIndicator note">
<p>当使用一个 `regex`时,您必须设置全局(“ g”)标志,<br>
 否则,它将引发 <code>TypeError</code>:“必须使用全局 RegExp 调用 replaceAll”。</p>
</div>

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

<dl>
 <dt><code><var>regexp</var></code> (pattern)</dt>
 <dd>A {{jsxref("RegExp")}} object or literal with the global flag. The matches are replaced with <code><var>newSubstr</var></code> or the value returned by the specified <code><var>function</var></code>. A RegExp without the global ("g") flag will throw a <code>TypeError</code>: "replaceAll must be called with a global RegExp".</dd>
 <dt><code><var>substr</var></code></dt>
 <dd>A {{jsxref("String")}} that is to be replaced by <code><var>newSubstr</var></code>. It is treated as a literal string and is <em>not</em> interpreted as a regular expression.</dd>
 <dt><code><var>newSubstr</var></code> (replacement)</dt>
 <dd>The {{jsxref("String")}} that replaces the substring specified by the specified <code><var>regexp</var></code> or <code><var>substr</var></code> parameter. A number of special replacement patterns are supported; see the "<a href="#Specifying_a_string_as_a_parameter">Specifying a string as a parameter</a>" section below.</dd>
 <dt><code><var>function</var></code> (replacement)</dt>
 <dd>A function to be invoked to create the new substring to be used to replace the matches to the given <code><var>regexp</var></code> or <code><var>substr</var></code>. The arguments supplied to this function are described in the "<a href="#Specifying_a_function_as_a_parameter">Specifying a function as a parameter</a>" section below.</dd>
</dl>

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

<p>A new string, with all matches of a pattern replaced by a replacement.</p>

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

<p>此方法不会更改调用 {{jsxref("String")}} 对象。它只是返回一个新字符串。</p>

<h3 id="将一个字符串作为一个参数">将一个字符串作为一个参数</h3>

<p>The replacement string can include the following special replacement patterns:</p>

<table class="standard-table">
 <thead>
  <tr>
   <th class="header" scope="col">Pattern</th>
   <th class="header" scope="col">Inserts</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>$$</code></td>
   <td>Inserts a <code>"$"</code>.</td>
  </tr>
  <tr>
   <td><code>$&amp;</code></td>
   <td>Inserts the matched substring.</td>
  </tr>
  <tr>
   <td><code>$`</code></td>
   <td>Inserts the portion of the string that precedes the matched substring.</td>
  </tr>
  <tr>
   <td><code>$'</code></td>
   <td>Inserts the portion of the string that follows the matched substring.</td>
  </tr>
  <tr>
   <td><code>$<var>n</var></code></td>
   <td>Where <code><var>n</var></code> is a positive integer less than 100, inserts the <code><var>n</var></code>th parenthesized submatch string, provided the first argument was a {{jsxref("RegExp")}} object. Note that this is <code>1</code>-indexed.</td>
  </tr>
 </tbody>
</table>

<h3 id="将一个函数指定为一个参数">将一个函数指定为一个参数</h3>

<p>你可以指定一个函数作为第二个参数,在这种情况下,函数只有在匹配发生之后才会被调用。The function's result (return value) will be used as the replacement string. (<strong>Note:</strong> The above-mentioned special replacement patterns do <em>not</em> apply in this case.)</p>

<p>Note that the function will be invoked multiple times for each full match to be replaced if the regular expression in the first parameter is global.</p>

<p>The arguments to the function are as follows:</p>

<table class="standard-table">
 <thead>
  <tr>
   <th class="header" scope="col">Possible name</th>
   <th class="header" scope="col">Supplied value</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>match</code></td>
   <td>The matched substring. (Corresponds to <code>$&amp;</code> above.)</td>
  </tr>
  <tr>
   <td><code>p1, p2, ...</code></td>
   <td>The <var>n</var>th string found by a parenthesized capture group, provided the first argument to <code>replace()</code> was a {{jsxref("RegExp")}} object. (Corresponds to <code>$1</code>, <code>$2</code>, etc. above.) For example, if <code>/(\a+)(\b+)/</code>, was given, <code>p1</code> is the match for <code>\a+</code>, and <code>p2</code> for <code>\b+</code>.</td>
  </tr>
  <tr>
   <td><code>offset</code></td>
   <td>The offset of the matched substring within the whole string being examined. (For example, if the whole string was <code>'abcd'</code>, and the matched substring was <code>'bc'</code>, then this argument will be <code>1</code>.)</td>
  </tr>
  <tr>
   <td><code>string</code></td>
   <td>The whole string being examined.</td>
  </tr>
 </tbody>
</table>

<p>(The exact number of arguments depends on whether the first argument is a {{jsxref("RegExp")}} object—and, if so, how many parenthesized submatches it specifies.)</p>

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

<h3 id="使用_replaceAll">使用 replaceAll</h3>

<pre class="brush: js">'aabbcc'.replaceAll('b', '.');
// 'aa..cc'</pre>

<h3 id="非全局_regex_抛出">非全局 regex 抛出</h3>

<p>使用正则表达式搜索值时,它必须是全局的。这将行不通:</p>

<pre class="brush: js; example-bad">'aabbcc'.replaceAll(/b/, '.');
TypeError: replaceAll must be called with a global RegExp
</pre>

<p>这将可以正常运行:</p>

<pre class="brush: js; example-good">'aabbcc'.replaceAll(/b/g, '.');
"aa..cc"
</pre>

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-string.prototype.replaceall', 'String.prototype.replaceAll')}}</td>
  </tr>
 </tbody>
</table>

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

<p>{{Compat("javascript.builtins.String.replaceAll")}}</p>

<h2 id="了解更多">了解更多</h2>

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