aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/matchall
diff options
context:
space:
mode:
authorIrvin <irvinfly@gmail.com>2022-02-16 02:02:49 +0800
committerIrvin <irvinfly@gmail.com>2022-02-16 02:35:54 +0800
commit01b0e12ba27b5069248fd09235e9a7143915ee30 (patch)
tree0e9edf538dc3fa3331e1dbb79239b58186765f86 /files/zh-cn/web/javascript/reference/global_objects/string/matchall
parent6ca84f1794af830ada9736d7289ce29aabb04ca3 (diff)
downloadtranslated-content-01b0e12ba27b5069248fd09235e9a7143915ee30.tar.gz
translated-content-01b0e12ba27b5069248fd09235e9a7143915ee30.tar.bz2
translated-content-01b0e12ba27b5069248fd09235e9a7143915ee30.zip
remove `notranslate` class in zh-CN
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/string/matchall')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/string/matchall/index.html14
1 files changed, 7 insertions, 7 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/matchall/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/matchall/index.html
index f2344f6f79..d11d23265e 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/matchall/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/matchall/index.html
@@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/matchAll
<h2 id="语法">语法</h2>
-<pre class="syntaxbox notranslate"><var>str</var>.matchAll(<var>regexp</var>)</pre>
+<pre class="syntaxbox"><var>str</var>.matchAll(<var>regexp</var>)</pre>
<h3 id="参数">参数</h3>
@@ -42,7 +42,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/matchAll
<p>在 <code>matchAll</code> 出现之前,通过在循环中调用 <code>regexp.exec()</code> 来获取所有匹配项信息(regexp 需使用 <code>/g</code> 标志):</p>
-<pre class="brush: js notranslate">const regexp = RegExp('foo[a-z]*','g');
+<pre class="brush: js">const regexp = RegExp('foo[a-z]*','g');
const str = 'table football, foosball';
let match;
@@ -54,7 +54,7 @@ while ((match = regexp.exec(str)) !== null) {
<p>如果使用 <code>matchAll</code> ,就可以不必使用 while 循环加 exec 方式(且正则表达式需使用 <code>/g</code> 标志)。使用 <code>matchAll</code> 会得到一个迭代器的返回值,配合 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">for...of</a></code>, <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax">array spread</a>, 或者 {{jsxref("Array.from()")}} 可以更方便实现功能:</p>
-<pre class="brush: js notranslate">const regexp = RegExp('foo[a-z]*','g');
+<pre class="brush: js">const regexp = RegExp('foo[a-z]*','g');
const str = 'table football, foosball';
const matches = str.matchAll(regexp);
@@ -71,14 +71,14 @@ Array.from(str.matchAll(regexp), m =&gt; m[0]);
<p>如果没有 <code>/g</code> 标志,<code>matchAll</code> 会抛出异常。</p>
-<pre class="brush: js notranslate">const regexp = RegExp('[a-c]','');
+<pre class="brush: js">const regexp = RegExp('[a-c]','');
const str = 'abc';
Array.from(str.matchAll(regexp), m =&gt; m[0]);
// TypeError: String.prototype.matchAll called with a non-global RegExp argument</pre>
<p><code>matchAll</code> 内部做了一个 regexp 的复制,所以不像 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec">regexp.exec</a>, <code>lastIndex</code> 在字符串扫描时不会改变。</p>
-<pre class="brush: js notranslate">const regexp = RegExp('[a-c]','g');
+<pre class="brush: js">const regexp = RegExp('[a-c]','g');
regexp.lastIndex = 1;
const str = 'abc';
Array.from(str.matchAll(regexp), m =&gt; `${regexp.lastIndex} ${m[0]}`);
@@ -89,7 +89,7 @@ Array.from(str.matchAll(regexp), m =&gt; `${regexp.lastIndex} ${m[0]}`);
<p><code>matchAll</code> 的另外一个亮点是更好地获取捕获组。因为当使用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match">match()</a></code> 和 <code>/g</code> 标志方式获取匹配信息时,捕获组会被忽略:</p>
-<pre class="brush: js notranslate">var regexp = /t(e)(st(\d?))/g;
+<pre class="brush: js">var regexp = /t(e)(st(\d?))/g;
var str = 'test1test2';
str.match(regexp);
@@ -97,7 +97,7 @@ str.match(regexp);
<p>使用 <code>matchAll</code> 可以通过如下方式获取分组捕获:</p>
-<pre class="brush: js notranslate">let array = [...str.matchAll(regexp)];
+<pre class="brush: js">let array = [...str.matchAll(regexp)];
array[0];
// ['test1', 'e', 'st1', '1', index: 0, input: 'test1test2', length: 4]