I agree that execAll() is not a 100% winner, more like a clean-up of a quirky corner. But exec() in “multi” mode has a surprising amount of pitfalls:
* /g flag must be set * lastIndex must be 0 * can’t inline the regex, because it is needed as a pseudo-iterator (more of an anti-pattern, anyway, but still) * side effects via lastIndex may be a problem All of these would go away with a execAll(). The thing I’m not sure about is how frequently exec() is used that way. String.prototype.match() does indeed cover a lot of use cases. So does String.prototype.replace(). On Aug 29, 2013, at 9:45 , Brendan Eich <bren...@mozilla.com> wrote: > 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 > -- Dr. Axel Rauschmayer a...@rauschma.de home: rauschma.de twitter: twitter.com/rauschma blog: 2ality.com
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss