Letting RegExp method return something iterable?

Brendan Eich brendan at mozilla.com
Thu Aug 29 00:45:45 PDT 2013


Axel Rauschmayer wrote:
>> The fact that s.match(/re/g) returns the array of all matches (with 
>> captures) sucks some of the oxygen away from any /re/g.execAll(s) 
>> proposal.
>>
>> But String.prototype.match has perlish hair (e.g., those capture 
>> groups showing up in the result array).
>
> Really? AFAICT, only the complete matches (group 0) are returned.
>

Sorry, of course you are right -- how soon I forget -- the subgroups 
show up only in each exec result array, but are dropped from the match 
result.

So hair on the other side of the coin, if you will. A naive iterator 
that calls exec would return, e.g.,["ab", "b"] for the first iteration 
given r and s as follows:

js> r = /.(.)/g
/.(.)/g
js> s = 'abcdefgh'
"abcdefgh"
js> a = s.match(r)
["ab", "cd", "ef", "gh"]
js> b = r.exec(s)
["ab", "b"]

Is this what the programmer wants? If not, String.prototype.match stands 
ready, and again takes away motivation for an eager execAll.

But programmers wanting exec with submatches could use a lazy form:

js> r.lastIndex = 0
0
js> RegExp.prototype.execAll = function (s) { let m; while (m = 
this.exec(s)) yield m; }
(function (s) { let m; while (m = this.exec(s)) yield m; })
js> c = [m for (m of r.execAll(s))]
[["ab", "b"], ["cd", "d"], ["ef", "f"], ["gh", "h"]]

/be


More information about the es-discuss mailing list