Skip to content

Commit 85f514b

Browse files
authored
fix: examples in regexp/@@matchall/index.md (mdn#19898)
- [x] fix typo from `.match` to `.matchAll` - [x] fix erroneous outputs from a couple examples (expected output was that of `.match`, not `.matchAll`) - [x] change some single quotes to double, add padding around array literal brackets (for consistent style) - [x] move some of the expected output comments to new lines (for improved readability) - [x] fix some other minor grammar and syntax issues
1 parent c091134 commit 85f514b

File tree

1 file changed

+25
-17
lines changed
  • files/en-us/web/javascript/reference/global_objects/regexp/@@matchall

1 file changed

+25
-17
lines changed

files/en-us/web/javascript/reference/global_objects/regexp/@@matchall/index.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,38 @@ An [iterable iterator](/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
3737
This method is called internally in {{jsxref("String.prototype.matchAll()")}}. For example, the following two examples return the same result.
3838

3939
```js
40-
'abc'.matchAll(/a/g);
40+
"abc".matchAll(/a/g);
4141

42-
/a/g[Symbol.matchAll]('abc');
42+
/a/g[Symbol.matchAll]("abc");
4343
```
4444

4545
Like [`@@split`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@split), `@@matchAll` starts by using [`@@species`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@species) to construct a new regex, thus avoiding mutating the original regexp in any way. [`lastIndex`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) starts as the original regex's value.
4646

4747
```js
4848
const regexp = /[a-c]/g;
4949
regexp.lastIndex = 1;
50-
const str = 'abc';
50+
const str = "abc";
5151
Array.from(str.matchAll(regexp), (m) => `${regexp.lastIndex} ${m[0]}`);
52-
// Array [ "1 b", "1 c" ]
52+
// [ "1 b", "1 c" ]
5353
```
5454

5555
The validation that the input is a global regex happens in [`String.prototype.matchAll()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll). `@@matchAll` does not validate the input. If the regex is not global, the returned iterator yields the [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) result once and then returns `undefined`. If the regexp is global, each time the returned iterator's `next()` method is called, the regex's [`exec()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec) is called and the result is yielded.
5656

57-
When the regex is sticky and global, it would still perform sticky matches — i.e. it would fail to match any occurrences beyond the `lastIndex`.
57+
When the regex is sticky and global, it will still perform sticky matches — i.e. it will not match any occurrences beyond the `lastIndex`.
5858

5959
```js
60-
console.log(Array.from("ab-c".matchAll(/[abc]/gy))); // [ 'a', 'b' ]
60+
console.log(Array.from("ab-c".matchAll(/[abc]/gy)));
61+
// [ [ "a" ], [ "b" ] ]
6162
```
6263

63-
If the current match is an empty string, the [`lastIndex`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) would still be advanced — if the regex has the [`u`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode) flag, it would advance by one Unicode codepoint; otherwise, it advances by one UTF-16 code unit.
64+
If the current match is an empty string, the [`lastIndex`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) will still be advanced. If the regex has the [`u`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode) flag, it advances by one Unicode codepoint; otherwise, it advances by one UTF-16 codepoint.
6465

6566
```js
66-
console.log(Array.from("😄".matchAll(/(?:)/g))); // [ '', '', '' ]
67-
console.log(Array.from("😄".match(/(?:)/gu))); // [ '', '' ]
67+
console.log(Array.from("😄".matchAll(/(?:)/g)));
68+
// [ [ "" ], [ "" ], [ "" ] ]
69+
70+
console.log(Array.from("😄".matchAll(/(?:)/gu)));
71+
// [ [ "" ], [ "" ] ]
6872
```
6973

7074
This method exists for customizing the behavior of `matchAll()` in {{jsxref('RegExp')}} subclasses.
@@ -77,11 +81,11 @@ This method can be used in almost the same way as {{jsxref("String.prototype.mat
7781

7882
```js
7983
const re = /[0-9]+/g;
80-
const str = '2016-01-02';
84+
const str = "2016-01-02";
8185
const result = re[Symbol.matchAll](str);
8286

8387
console.log(Array.from(result, (x) => x[0]));
84-
// ["2016", "01", "02"]
88+
// [ "2016", "01", "02" ]
8589
```
8690

8791
### Using @@matchAll in subclasses
@@ -98,11 +102,15 @@ class MyRegExp extends RegExp {
98102
}
99103
}
100104

101-
const re = new MyRegExp('([0-9]+)-([0-9]+)-([0-9]+)', 'g');
102-
const str = '2016-01-02|2019-03-07';
105+
const re = new MyRegExp("([0-9]+)-([0-9]+)-([0-9]+)", "g");
106+
const str = "2016-01-02|2019-03-07";
103107
const result = str.matchAll(re);
104-
console.log(result[0]); // [ "2016-01-02", "2016", "01", "02" ]
105-
console.log(result[1]); // [ "2019-03-07", "2019", "03", "07" ]
108+
109+
console.log(result[0]);
110+
// [ "2016-01-02", "2016", "01", "02" ]
111+
112+
console.log(result[1]);
113+
// [ "2019-03-07", "2019", "03", "07" ]
106114
```
107115

108116
## Specifications
@@ -116,5 +124,5 @@ console.log(result[1]); // [ "2019-03-07", "2019", "03", "07" ]
116124
## See also
117125

118126
- [Polyfill of `RegExp.prototype[@@matchAll]` in `core-js`](https://github.com/zloirock/core-js#ecmascript-string-and-regexp)
119-
- {{JSxRef("String.prototype.matchAll()")}}
120-
- {{JSxRef("Symbol.matchAll")}}
127+
- {{jsxref("String.prototype.matchAll()")}}
128+
- {{jsxref("Symbol.matchAll")}}

0 commit comments

Comments
 (0)