diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/string/replaceall/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/string/replaceall/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/string/replaceall/index.html | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/replaceall/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/replaceall/index.html new file mode 100644 index 0000000000..b79277e488 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/string/replaceall/index.html @@ -0,0 +1,171 @@ +--- +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><span class="seoSummary"><strong><code>replaceAll()</code></strong> 方法返回一个新字符串,新字符串所有满足 <code>pattern</code> 的部分都已被<code>replacement</code> 替换。</span><span class="seoSummary"><code>pattern</code></span>可以是一个字符串或一个 {{jsxref("RegExp")}}, <code>replacement</code>可以是一个字符串或一个在每次匹配被调用的函数。</p> + +<p>原始字符串保持不变。</p> + +<div>{{EmbedInteractiveExample("pages/js/string-replaceall.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">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>$&</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>$&</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 notranslate">'aabbcc'.replaceAll('b', '.'); +// 'aa..cc'</pre> + +<h3 id="非全局_regex_抛出">非全局 regex 抛出</h3> + +<p>使用正则表达式搜索值时,它必须是全局的。这将行不通:</p> + +<pre class="brush: js; example-bad notranslate">'aabbcc'.replaceAll(/b/, '.'); +TypeError: replaceAll must be called with a global RegExp +</pre> + +<p>这将可以正常运行:</p> + +<pre class="brush: js; example-good notranslate">'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 class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<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> |