├── .gitattributes ├── .gitignore ├── .npmrc ├── LICENSE ├── README.md ├── index.html ├── package.json ├── spec.emu └── spec.md /.gitattributes: -------------------------------------------------------------------------------- 1 | index.html -diff merge=ours 2 | index.html linguist-generated=true 3 | spec.js -diff merge=ours 4 | spec.js linguist-generated=true 5 | spec.css -diff merge=ours 6 | spec.css linguist-generated=true 7 | package.json trigger=true 8 | spec.emu trigger=true 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | 29 | # Only apps should have lockfiles 30 | npm-shrinkwrap.json 31 | package-lock.json 32 | yarn.lock 33 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jordan Harband 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # String.prototype.matchAll 2 | Proposal and specs for String.prototype.matchAll. 3 | 4 | ## Polyfill/Shim 5 | See [string.prototype.matchall on npm](https://www.npmjs.com/package/string.prototype.matchall) or on [github](https://github.com/ljharb/String.prototype.matchAll). 6 | 7 | ## Spec 8 | You can view the spec in [markdown format](spec.md) or rendered as [HTML](https://tc39.github.io/proposal-string-matchall/). 9 | 10 | ## Rationale 11 | If I have a string, and either a sticky or a global regular expression which has multiple capturing groups, I often want to iterate through all of the matches. 12 | Currently, my options are the following: 13 | ```js 14 | var regex = /t(e)(st(\d?))/g; 15 | var string = 'test1test2'; 16 | 17 | string.match(regex); // gives ['test1', 'test2'] - how do i get the capturing groups? 18 | 19 | var matches = []; 20 | var lastIndexes = {}; 21 | var match; 22 | lastIndexes[regex.lastIndex] = true; 23 | while (match = regex.exec(string)) { 24 | lastIndexes[regex.lastIndex] = true; 25 | matches.push(match); 26 | // example: ['test1', 'e', 'st1', '1'] with properties `index` and `input` 27 | } 28 | matches; /* gives exactly what i want, but uses a loop, 29 | * and mutates the regex's `lastIndex` property */ 30 | lastIndexes; /* ideally should give { 0: true } but instead 31 | * will have a value for each mutation of lastIndex */ 32 | 33 | var matches = []; 34 | string.replace(regex, function () { 35 | var match = Array.prototype.slice.call(arguments, 0, -2); 36 | match.input = arguments[arguments.length - 1]; 37 | match.index = arguments[arguments.length - 2]; 38 | matches.push(match); 39 | // example: ['test1', 'e', 'st1', '1'] with properties `index` and `input` 40 | }); 41 | matches; /* gives exactly what i want, but abuses `replace`, 42 | * mutates the regex's `lastIndex` property, 43 | * and requires manual construction of `match` */ 44 | ``` 45 | 46 | The first example does not provide the capturing groups, so isn’t an option. The latter two examples both visibly mutate `lastIndex` - this is not a huge issue (beyond ideological) with built-in `RegExp`s, however, with subclassable `RegExp`s in ES6/ES2015, this is a bit of a messy way to obtain the desired information on all matches. 47 | 48 | Thus, `String#matchAll` would solve this use case by both providing access to all of the capturing groups, and not visibly mutating the regular expression object in question. 49 | 50 | ## Iterator versus Array 51 | Many use cases may want an array of matches - however, clearly not all will. Particularly large numbers of capturing groups, or large strings, might have performance implications to always gather all of them into an array. By returning an iterator, it can trivially be collected into an array with the spread operator or `Array.from` if the caller wishes to, but it need not. 52 | 53 | ## Previous discussions 54 | - http://blog.stevenlevithan.com/archives/fixing-javascript-regexp 55 | - https://esdiscuss.org/topic/letting-regexp-method-return-something-iterable 56 | - http://stackoverflow.com/questions/844001/javascript-regular-expressions-and-sub-matches 57 | - http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regular-expression 58 | - http://stackoverflow.com/questions/19913667/javascript-regex-global-match-groups 59 | - http://stackoverflow.com/questions/844001/javascript-regular-expressions-and-sub-matches 60 | - http://blog.getify.com/to-capture-or-not/#manually-splitting-string 61 | 62 | ## Naming 63 | The name `matchAll` was selected to correspond with `match`, and to connote that *all* matches would be returned, not just a single match. This includes the connotation that the provided regex will be used with a global flag, to locate all matches in the string. An alternate name has been suggested, `matches` - this follows the precedent set by `keys`/`values`/`entries`, which is that a plural noun indicates that it returns an iterator. However, `includes` returns a boolean. When the word is not unambiguously a noun or a verb, "plural noun" doesn't seem as obvious a convention to follow. 64 | 65 | Update from committee feedback: ruby uses the word `scan` for this, but the committee is not comfortable introducing a new word to JavaScript. `matchEach` was suggested, but some were not comfortable with the naming similarity to `forEach` while the API was quite different. `matchAll` seems to be the name everyone is most comfortable with. 66 | 67 | In the September 2017 TC39 meeting, there was a question raised about whether "all" means "all overlapping matches" or "all non-overlapping matches" - where “overlapping” means “all matches starting from each character in the string”, and “non-overlapping” means “all matches starting from the beginning of the string”. We briefly considered either renaming the method, or adding a way to achieve both semantics, but the objection was withdrawn. 68 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | String.prototype.matchAll 9 | 138 | 1078 | 2173 | 2174 | 2175 | 2176 | 2177 | 2178 | 2205 |
2206 |

Stage 3 Draft / December 7, 2018

2207 |

String.prototype.matchAll

2208 | 2209 | 2210 | 2211 |

1String.prototype.matchAll ( regexp )

2212 | 2213 |

Performs a regular expression match of the String representing the this value against regexp and returns an iterator. Each iteration result’s value is an Array object containing the results of the match, or null if the String did not match.

2214 | 2215 |

When the matchAll method is called, the following steps are taken:

2216 |
  1. Let O be ? RequireObjectCoercible(this value).
  2. If regexp is neither undefined nor null, then
    1. Let matcher be ? GetMethod(regexp, @@matchAll).
    2. If matcher is not undefined, then
      1. Return ? Call(matcher, regexp, « O »).
  3. Let S be ? ToString(O).
  4. Let rx be ? RegExpCreate(regexp, "g").
  5. Return ? Invoke(rx, @@matchAll, « S »). 2217 |
2218 | Note 1
The matchAll function is intentionally generic, it does not require that its this value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method.
2219 | Note 2
Similarly to String.prototype.split, String.prototype.matchAll is designed to typically act without mutating its inputs.
2220 |
2221 |
2222 | 2223 | 2224 | 2225 |

2RegExp.prototype [ @@matchAll ] ( string )

2226 | 2227 |

When the @@matchAll method is called with argument string, the following steps are taken:

2228 |
  1. Let R be the this value.
  2. If Type(R) is not Object, throw a TypeError exception.
  3. Let S be ? ToString(string).
  4. Let C be ? SpeciesConstructor(R, %RegExp%).
  5. Let flags be ? ToString(? Get(R, "flags")).
  6. Let matcher be ? Construct(C, « R, flags »).
  7. Let lastIndex be ? ToLength(? Get(R, "lastIndex")).
  8. Perform ? Set(matcher, "lastIndex", lastIndex, true).
  9. If flags contains "g", let global be true.
  10. Else, let global be false.
  11. If flags contains "u", let fullUnicode be true.
  12. Else, let fullUnicode be false.
  13. Return ! CreateRegExpStringIterator(matcher, S, global, fullUnicode). 2229 |
2230 |

The value of the name property of this function is "[Symbol.matchAll]".

2231 |
2232 |
2233 | 2234 | 2235 | 2236 |

3CreateRegExpStringIterator ( R, S, global, fullUnicode )

2237 | 2238 |

The abstract operation CreateRegExpStringIterator is used to create such iterator objects. It performs the following steps:

2239 |
  1. Assert: Type(S) is String.
  2. Assert: Type(global) is Boolean.
  3. Assert: Type(fullUnicode) is Boolean.
  4. Let iterator be ObjectCreate(%RegExpStringIteratorPrototype%, « [[IteratingRegExp]], [[IteratedString]], [[Global]], [[Unicode]], [[Done]] »).
  5. Set iterator.[[IteratingRegExp]] to R.
  6. Set iterator.[[IteratedString]] to S.
  7. Set iterator.[[Global]] to global.
  8. Set iterator.[[Unicode]] to fullUnicode.
  9. Set iterator.[[Done]] to false.
  10. Return iterator. 2240 |
2241 |
2242 |
2243 | 2244 | 2245 | 2246 |

4The %RegExpStringIteratorPrototype% Object

2247 | 2248 |

All RegExp String Iterator Objects inherit properties from the %RegExpStringIteratorPrototype% intrinsic object. The %RegExpStringIteratorPrototype% object is an ordinary object and its [[Prototype]] internal slot is the %IteratorPrototype% intrinsic object. In addition, %RegExpStringIteratorPrototype% has the following properties:

2249 | 2250 | 2251 |

4.1%RegExpStringIteratorPrototype%.next ( )

2252 |
  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have all of the internal slots of a RegExp String Iterator Object Instance (see 4.3), throw a TypeError exception.
  4. If O.[[Done]] is true, then
    1. Return ! CreateIterResultObject(undefined, true).
  5. Let R be O.[[IteratingRegExp]].
  6. Let S be O.[[IteratedString]].
  7. Let global be O.[[Global]].
  8. Let fullUnicode be O.[[Unicode]].
  9. Let match be ? RegExpExec(R, S).
  10. If match is null, then
    1. Set O.[[Done]] to true.
    2. Return ! CreateIterResultObject(undefined, true).
  11. Else,
    1. If global is true,
      1. Let matchStr be ? ToString(? Get(match, "0")).
      2. If matchStr is the empty string,
        1. Let thisIndex be ? ToLength(? Get(R, "lastIndex")).
        2. Let nextIndex be ! AdvanceStringIndex(S, thisIndex, fullUnicode).
        3. Perform ? Set(R, "lastIndex", nextIndex, true).
      3. Return ! CreateIterResultObject(match, false).
    2. Else,
      1. Set O.[[Done]] to true.
      2. Return ! CreateIterResultObject(match, false). 2253 |
2254 |
2255 | 2256 | 2257 |

4.2%RegExpStringIteratorPrototype%[ @@toStringTag ]

2258 |

The initial value of the @@toStringTag property is the String value "RegExp String Iterator".

2259 |

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.

2260 |
2261 | 2262 | 2263 |

4.3Properties of RegExp String Iterator Instances

2264 |

RegExp String Iterator instances are ordinary objects that inherit properties from the %RegExpStringIteratorPrototype% intrinsic object. RegExp String Iterator instances are initially created with the internal slots listed in Table 1.

2265 |
2266 |
Table 1 – Internal Slots of RegExp String Iterator Instances
2267 | 2268 | 2269 | 2270 | 2271 | 2272 | 2273 | 2274 | 2275 | 2276 | 2277 | 2278 | 2279 | 2280 | 2281 | 2282 | 2283 | 2284 | 2285 | 2286 | 2287 | 2288 | 2289 | 2290 | 2291 | 2292 | 2293 | 2294 | 2295 |
Internal SlotDescription
[[IteratingRegExp]]The regular expression used for iteration. IsRegExp([[IteratingRegExp]]) is always initially true.
[[IteratedString]]The String value being iterated upon.
[[Global]]A Boolean value to indicate whether the [[IteratingRegExp]] is global or not.
[[Unicode]]A Boolean value to indicate whether the [[IteratingRegExp]] is in Unicode mode or not.
[[Done]]A Boolean value to indicate whether the iteration is complete or not.
2296 |
2297 |
2298 |
2299 |
2300 | 2301 | 2302 | 2303 |

5Symbol.matchAll

2304 |

The initial value of Symbol.matchAll is the well-known symbol @@matchAll (Table 1).

2305 |

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

2306 |
2307 |
2308 | 2309 | 2310 |

6Well-Known Symbols

2311 | Editor's Note 2312 |
insert after @@match; before @@replace
2313 |
2314 | 2315 |
2316 |
Table 1: Well-known Symbols
2317 | 2318 | 2319 | 2320 | 2321 | 2322 | 2323 | 2324 | 2325 | 2326 | 2327 | 2328 | 2329 | 2330 |
Specification Name[[Description]]Value and Purpose
@@matchAll"Symbol.matchAll"A regular expression method that returns an iterator, that yields matches of the regular expression against a string. Called by the String.prototype.matchAll method.
2331 |
2332 |
2333 |
2334 | 2335 |

ACopyright & Software License

2336 | 2337 |

Copyright Notice

2338 |

© 2018 Jordan Harband

2339 | 2340 |

Software License

2341 |

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

2342 | 2343 |

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

2344 | 2345 |
    2346 |
  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. 2347 |
  3. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  4. 2348 |
  5. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.
  6. 2349 |
2350 | 2351 |

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

2352 | 2353 |
2354 | 2355 |
2356 | 2357 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "string-matchall", 3 | "private": true, 4 | "version": "0.0.0", 5 | "description": "Spec-compliant polyfill for String.prototype.matchAll ESnext proposal.", 6 | "scripts": { 7 | "build": "ecmarkup spec.emu | js-beautify -f - --type=html -t | sed -e 's/ / /g' -e 's/[[:space:]]*$//' > index.html" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/tc39/proposal-string-matchall.git" 12 | }, 13 | "author": "Jordan Harband ", 14 | "license": "MIT", 15 | "devDependencies": { 16 | "ecmarkup": "^3.16.0", 17 | "js-beautify": "^1.7.5" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /spec.emu: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
  5 | title: String.prototype.matchAll
  6 | stage: 3
  7 | contributors: Jordan Harband
  8 | 
9 | 10 | 11 | 12 |

String.prototype.matchAll ( _regexp_ )

13 | 14 |

Performs a regular expression match of the String representing the *this* value against _regexp_ and returns an iterator. Each iteration result’s value is an Array object containing the results of the match, or *null* if the String did not match.

15 | 16 |

When the `matchAll` method is called, the following steps are taken:

17 | 18 | 1. Let _O_ be ? RequireObjectCoercible(*this* value). 19 | 1. If _regexp_ is neither *undefined* nor *null*, then 20 | 1. Let _matcher_ be ? GetMethod(_regexp_, @@matchAll). 21 | 1. If _matcher_ is not *undefined*, then 22 | 1. Return ? Call(_matcher_, _regexp_, « _O_ »). 23 | 1. Let _S_ be ? ToString(_O_). 24 | 1. Let _rx_ be ? RegExpCreate(_regexp_, `"g"`). 25 | 1. Return ? Invoke(_rx_, @@matchAll, « _S_ »). 26 | 27 | The `matchAll` function is intentionally generic, it does not require that its *this* value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method. 28 | Similarly to `String.prototype.split`, `String.prototype.matchAll` is designed to typically act without mutating its inputs. 29 |
30 |
31 | 32 | 33 | 34 |

RegExp.prototype [ @@matchAll ] ( _string_ )

35 | 36 |

When the `@@matchAll` method is called with argument _string_, the following steps are taken:

37 | 38 | 1. Let _R_ be the *this* value. 39 | 1. If Type(_R_) is not Object, throw a *TypeError* exception. 40 | 1. Let _S_ be ? ToString(_string_). 41 | 1. Let _C_ be ? SpeciesConstructor(_R_, %RegExp%). 42 | 1. Let _flags_ be ? ToString(? Get(_R_, `"flags"`)). 43 | 1. Let _matcher_ be ? Construct(_C_, « _R_, _flags_ »). 44 | 1. Let _lastIndex_ be ? ToLength(? Get(_R_, `"lastIndex"`)). 45 | 1. Perform ? Set(_matcher_, `"lastIndex"`, _lastIndex_, *true*). 46 | 1. If _flags_ contains `"g"`, let _global_ be *true*. 47 | 1. Else, let _global_ be *false*. 48 | 1. If _flags_ contains `"u"`, let _fullUnicode_ be *true*. 49 | 1. Else, let _fullUnicode_ be *false*. 50 | 1. Return ! CreateRegExpStringIterator(_matcher_, _S_, _global_, _fullUnicode_). 51 | 52 |

The value of the *name* property of this function is *"[Symbol.matchAll]"*.

53 |
54 |
55 | 56 | 57 | 58 |

CreateRegExpStringIterator ( _R_, _S_, _global_, _fullUnicode_ )

59 | 60 |

The abstract operation _CreateRegExpStringIterator_ is used to create such iterator objects. It performs the following steps:

61 | 62 | 1. Assert: Type(_S_) is String. 63 | 1. Assert: Type(_global_) is Boolean. 64 | 1. Assert: Type(_fullUnicode_) is Boolean. 65 | 1. Let _iterator_ be ObjectCreate(%RegExpStringIteratorPrototype%, « [[IteratingRegExp]], [[IteratedString]], [[Global]], [[Unicode]], [[Done]] »). 66 | 1. Set _iterator_.[[IteratingRegExp]] to _R_. 67 | 1. Set _iterator_.[[IteratedString]] to _S_. 68 | 1. Set _iterator_.[[Global]] to _global_. 69 | 1. Set _iterator_.[[Unicode]] to _fullUnicode_. 70 | 1. Set _iterator_.[[Done]] to *false*. 71 | 1. Return _iterator_. 72 | 73 |
74 |
75 | 76 | 77 | 78 |

The %RegExpStringIteratorPrototype% Object

79 | 80 |

All RegExp String Iterator Objects inherit properties from the %RegExpStringIteratorPrototype% intrinsic object. The %RegExpStringIteratorPrototype% object is an ordinary object and its [[Prototype]] internal slot is the %IteratorPrototype% intrinsic object. In addition, %RegExpStringIteratorPrototype% has the following properties:

81 | 82 | 83 |

%RegExpStringIteratorPrototype%.next ( )

84 | 85 | 1. Let _O_ be the *this* value. 86 | 1. If Type(_O_) is not Object, throw a *TypeError* exception. 87 | 1. If _O_ does not have all of the internal slots of a RegExp String Iterator Object Instance (see ), throw a *TypeError* exception. 88 | 1. If _O_.[[Done]] is *true*, then 89 | 1. Return ! CreateIterResultObject(*undefined*, *true*). 90 | 1. Let _R_ be _O_.[[IteratingRegExp]]. 91 | 1. Let _S_ be _O_.[[IteratedString]]. 92 | 1. Let _global_ be _O_.[[Global]]. 93 | 1. Let _fullUnicode_ be _O_.[[Unicode]]. 94 | 1. Let _match_ be ? RegExpExec(_R_, _S_). 95 | 1. If _match_ is *null*, then 96 | 1. Set _O_.[[Done]] to *true*. 97 | 1. Return ! CreateIterResultObject(*undefined*, *true*). 98 | 1. Else, 99 | 1. If _global_ is *true*, 100 | 1. Let _matchStr_ be ? ToString(? Get(_match_, *"0"*)). 101 | 1. If _matchStr_ is the empty string, 102 | 1. Let _thisIndex_ be ? ToLength(? Get(_R_, *"lastIndex"*)). 103 | 1. Let _nextIndex_ be ! AdvanceStringIndex(_S_, _thisIndex_, _fullUnicode_). 104 | 1. Perform ? Set(_R_, *"lastIndex"*, _nextIndex_, *true*). 105 | 1. Return ! CreateIterResultObject(_match_, *false*). 106 | 1. Else, 107 | 1. Set _O_.[[Done]] to *true*. 108 | 1. Return ! CreateIterResultObject(_match_, *false*). 109 | 110 |
111 | 112 | 113 |

%RegExpStringIteratorPrototype%[ @@toStringTag ]

114 |

The initial value of the _@@toStringTag_ property is the String value *"RegExp String Iterator"*.

115 |

This property has the attributes { [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *true* }.

116 |
117 | 118 | 119 |

Properties of RegExp String Iterator Instances

120 |

RegExp String Iterator instances are ordinary objects that inherit properties from the %RegExpStringIteratorPrototype% intrinsic object. RegExp String Iterator instances are initially created with the internal slots listed in Table 1.

121 |
122 |
Table 1 – Internal Slots of RegExp String Iterator Instances
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 |
Internal SlotDescription
[[IteratingRegExp]]The regular expression used for iteration. IsRegExp([[IteratingRegExp]]) is always initially *true*.
[[IteratedString]]The String value being iterated upon.
[[Global]]A Boolean value to indicate whether the [[IteratingRegExp]] is global or not.
[[Unicode]]A Boolean value to indicate whether the [[IteratingRegExp]] is in Unicode mode or not.
[[Done]]A Boolean value to indicate whether the iteration is complete or not.
152 |
153 |
154 |
155 |
156 | 157 | 158 | 159 |

Symbol.matchAll

160 |

The initial value of *Symbol.matchAll* is the well-known symbol @@matchAll ().

161 |

This property has the attributes { [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *false* }.

162 |
163 |
164 | 165 | 166 |

Well-Known Symbols

167 | insert after @@match; before @@replace 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 |
Specification Name[[Description]]Value and Purpose
@@matchAll`"Symbol.matchAll"`A regular expression method that returns an iterator, that yields matches of the regular expression against a string. Called by the `String.prototype.matchAll` method.
183 |
184 |
185 | 186 | -------------------------------------------------------------------------------- /spec.md: -------------------------------------------------------------------------------- 1 | # String.prototype.matchAll ( *regexp* ) 2 | 3 | Performs a regular expression match of the String representing the **this** value against *regexp* and returns an iterator. Each iteration result’s value is an Array object containing the results of the match, or **null** if the String did not match. 4 | 5 | When the `matchAll` method is called, the following steps are taken: 6 | 1. Let *O* be ? [RequireObjectCoercible][require-object-coercible](**this** value). 7 | 1. If *regexp* is neither **undefined** nor **null**, then 8 | 1. Let *matcher* be ? [GetMethod][getmethod](*regexp*, @@matchAll). 9 | 1. If *matcher* is not **undefined**, then 10 | 1. Return ? [Call](call)(*matcher*, *regexp*, « *O* »). 11 | 1. Let *S* be ? [ToString][tostring](*O*). 12 | 1. Let *rx* be ? [RegExpCreate][regexp-create](*regexp*, `"g"`). 13 | 1. Return ? [Invoke][invoke](*rx*, @@matchAll, « *S* »). 14 | 15 | Note 1: The `matchAll` function is intentionally generic, it does not require that its *this* value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method. 16 | Note 2: Similarly to `String.prototype.split`, `String.prototype.matchAll` is designed to typically act without mutating its inputs. 17 | 18 | # RegExp.prototype[ @@matchAll ] ( *string* ) 19 | 20 | When the `@@matchAll` method is called with argument *string*, the following steps are taken: 21 | 1. Let *R* be the **this** value. 22 | 1. If [Type][type](_R_) is not Object, throw a **TypeError** exception. 23 | 1. Let *S* be ? [ToString][tostring](*string*). 24 | 1. Let *C* be ? [SpeciesConstructor][species-constructor](*R*, %RegExp%). 25 | 1. Let *flags* be ? [ToString][tostring](? [Get][get](*R*, `"flags"`)). 26 | 1. Let *matcher* be ? [Construct][construct](*C*, « *R*, *flags* »). 27 | 1. Let *lastIndex* be ? [ToLength][tolength](? [Get][get](*R*, `"lastIndex"`)). 28 | 1. Perform ? [Set][set](*matcher*, **"lastIndex"**, *lastIndex*, **true**). 29 | 1. If *flags* contains `"g"`, let *global* be **true**. 30 | 1. Else, let *global* be *false*. 31 | 1. If *flags* contains `"u"`, let *fullUnicode* be **true**. 32 | 1. Else, let *fullUnicode* be **false**. 33 | 1. Return ! [CreateRegExpStringIterator](#createregexpstringiterator-abstract-operation)(*matcher*, *S*, *global*, *fullUnicode*). 34 | 35 | The value of the name property of this function is "[Symbol.matchAll]". 36 | 37 | ## CreateRegExpStringIterator( *R*, *S*, *global*, *fullUnicode* ) 38 | 39 | The abstract operation *CreateRegExpStringIterator* is used to create such iterator objects. It performs the following steps: 40 | 1. Assert: [Type][type](*S*) is String. 41 | 1. Assert: [Type][type](*global*) is Boolean. 42 | 1. Assert: [Type][type](*unicode*) is Boolean. 43 | 1. Let *iterator* be ObjectCreate(%RegExpStringIteratorPrototype%, « [[IteratedString]], [[IteratingRegExp]], [[Global]], [[Unicode]], [[Done]] »). 44 | 1. Set *iterator*.[[IteratingRegExp]] to *R*. 45 | 1. Set *iterator*.[[IteratedString]] to *S*. 46 | 1. Set *iterator*.[[Global]] to *global*. 47 | 1. Set *iterator*.[[Unicode]] to *fullUnicode*. 48 | 1. Set *iterator*.[[Done]] to **true**. 49 | 1. Return *iterator*. 50 | 51 | ### The %RegExpStringIteratorPrototype% Object 52 | 53 | All RegExp String Iterator Objects inherit properties from the [%RegExpStringIteratorPrototype%](#the-regexpstringiteratorprototype-object) intrinsic object. The %RegExpStringIteratorPrototype% object is an ordinary object and its [[Prototype]] [internal slot][internal-slot] is the [%IteratorPrototype%][iterator-prototype] intrinsic object. In addition, %RegExpStringIteratorPrototype% has the following properties: 54 | 55 | #### %RegExpStringIteratorPrototype%.next ( ) 56 | 1. Let O be the **this** value. 57 | 1. If [Type][type](O) is not Object, throw a **TypeError** exception. 58 | 1. If O does not have all of the internal slots of a RegExp String Iterator Object Instance (see [here](#PropertiesOfRegExpStringIteratorInstances)), throw a **TypeError** exception. 59 | 1. If *O*.[[Done]] is **true**, then 60 | 1. Return ! [CreateIterResultObject][create-iter-result-object](**undefined**, **true**). 61 | 1. Let *R* be *O*.[[IteratingRegExp]]. 62 | 1. Let *S* be *O*.[[IteratedString]]. 63 | 1. Let *global* be *O*.[[Global]]. 64 | 1. Let *fullUnicode* be *O*.[[Unicode]]. 65 | 1. Let *match* be ? [RegExpExec][regexp-exec](*R*, *S*). 66 | 1. If *match* is **null**, then 67 | 1. Set *O*.[[Done]] to **true**. 68 | 1. Return ! [CreateIterResultObject][create-iter-result-object](**undefined**, **true**). 69 | 1. Else, 70 | 1. If *global* is **true**, 71 | 1. Let *matchStr* be ? [ToString][to-string](? [Get][get](*match*, **"0"**)). 72 | 1. If *matchStr* is the empty string, 73 | 1. Let *thisIndex* be ? [ToLength][tolength](? [Get][get](*R*, **"lastIndex"**). 74 | 1. Let *nextIndex* be ! AdvanceStringIndex(*S*, *thisIndex*, *fullUnicode*). 75 | 1. Perform ? [Set][set](*R*, **"lastIndex"**, *nextIndex*, **true**). 76 | 1. Return ! [CreateIterResultObject][create-iter-result-object](*match*, **false**). 77 | 1. Else, 78 | 1. Set *O*.[[Done]] to **true**. 79 | 1. Return ! [CreateIterResultObject][create-iter-result-object](_match_, **false**). 80 | 81 | #### %RegExpStringIteratorPrototype%[ @@toStringTag ] 82 | 83 | The initial value of the _@@toStringTag_ property is the String value **"RegExp String Iterator"**.

84 | This property has the attributes { [[Writable]]: **false**, [[Enumerable]]: **false**, [[Configurable]]: **true** }.

85 | 86 | #### Properties of RegExp String Iterator Instances 87 | 88 | RegExp String Iterator instances are ordinary objects that inherit properties from the [%RegExpStringIteratorPrototype%](#%RegExpStringIteratorPrototype%) intrinsic object. RegExp String Iterator instances are initially created with the internal slots listed in Table 1.

89 | 90 |
91 |
Table 1 — Internal Slots of RegExp String Iterator Instances
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 |
Internal SlotDescription
[[IteratingRegExp]]The regular expression used for iteration. [IsRegExp][isregexp]([[IteratingRegExp]]) is always initially true.
[[IteratedString]]The String value being iterated upon.
[[Global]]A Boolean value to indicate whether the [[IteratingRegExp]] is global or not.
[[Unicode]]A Boolean value to indicate whether the [[IteratingRegExp]] is in Unicode mode or not.
[[Done]]A Boolean value to indicate whether the iteration is complete or not.
120 |
121 | 122 | # Symbol.matchAll 123 | 124 | The initial value of *Symbol.matchAll* is the well-known symbol @@matchAll 125 | 126 | This property has the attributes { [[Writable]]: **false**, [[Enumerable]]: **false**, [[Configurable]]: **false** }. 127 | 128 | # Well-Known Symbols 129 | 130 |
131 |
Table 1: Well-known Symbols
132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 |
Specification Name[[Description]]Value and Purpose
insert after @@match
@@matchAll"Symbol.matchAll"A regular expression method that returns an iterator, that yields matches of the regular expression against a string. Called by the String.prototype.matchAll method.
insert before @@replace
154 |
155 | 156 | [to-string]: https://tc39.github.io/ecma262/#sec-tostring 157 | [regexp-create]: https://tc39.github.io/ecma262/#sec-regexpcreate 158 | [regexp-exec]: https://tc39.github.io/ecma262/#sec-regexpexec 159 | [require-object-coercible]: https://tc39.github.io/ecma262/#sec-requireobjectcoercible 160 | [internal-slot]: https://tc39.github.io/ecma262/#sec-object-internal-methods-and-internal-slots 161 | [type]: https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values 162 | [iterator-prototype]: https://tc39.github.io/ecma262/#sec-%iteratorprototype%-object 163 | [create-iter-result-object]: https://tc39.github.io/ecma262/#sec-createiterresultobject 164 | [isregexp]: https://tc39.github.io/ecma262/#sec-isregexp 165 | [object-create]: https://tc39.github.io/ecma262/#sec-objectcreate 166 | [call]: https://tc39.github.io/ecma262/#sec-call 167 | [species-constructor]: https://tc39.github.io/ecma262/#sec-speciesconstructor 168 | [construct]: https://tc39.github.io/ecma262/#sec-construct 169 | [tolength]: https://tc39.github.io/ecma262/#sec-tolength 170 | [set]: https://tc39.github.io/ecma262/#sec-set-o-p-v-throw 171 | [to-boolean]: https://tc39.github.io/ecma262/#sec-toboolean 172 | [invoke]: https://tc39.github.io/ecma262/#sec-invoke 173 | --------------------------------------------------------------------------------