Letting RegExp method return something iterable?

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon Aug 26 10:05:53 PDT 2013

you don't need to reset the `lastIndex` to zero if you don't break the loop
before unless you are sharing that regexp with some other part of code you
don't control.

What I am saying is that the example is very wrong as it is since there's
no way to have an unsafe `regexES5` behavior in there.

Moreover, if anyone uses the flag `g` to `test()` something is wrong unless
those properties you are complaining about or somebody might find silly are
actually used in a clever way that do not require the creation of Arrays
and garbage and track the position of each complex operation allowing
incremental parsers based on substrings to keep going and do a fast job
without bothering too much RAM and/or GC.

Long story short, I don't see any real use/case or any concrete advantage
with those examples so please make it more clear what's the problem you are
trying to solve and how these methods will concretely make our life easier

So far, and for what I can tell there, if you really need that Array you
can go probably faster _simply abusing replace_.

var re = /<(.*?)>/g; // also bad regexp for tags
                            // it grabs attributes too
function addMatch($0, $1) {
function createMatches(str) {
  var matches = [];
  str.replace(re, addMatch.bind(matches));
  return matches;

Above trick also scales more if you need to push more than a match in the

Although we might need a way to do similar thing without abusing other
methods but I keep guessing when this is so needed that should be in core
(1 line operation as your last one is ... anyone that needs that could
implement it without problems ;-))


On Sat, Aug 24, 2013 at 1:45 PM, Axel Rauschmayer <axel at rauschma.de> wrote:

> An example to make things clearer (thanks for the suggestion, Domenic):
> https://gist.github.com/rauschma/6330265
> On Aug 24, 2013, at 21:43 , Axel Rauschmayer <axel at rauschma.de> wrote:
> Well, obviously it doesn’t make much sense to do that for `text()`, but it
> would be great to have for `exec()`.
> On Aug 24, 2013, at 21:39 , Axel Rauschmayer <axel at rauschma.de> wrote:
> At the moment, the following two methods abuse regular expressions as
> iterators (if the /g flag is set):
> * `RegExp.prototype.test()`
> * `RegExp.prototype.exec()`
> Would it make sense to create similar methods that return something
> iterable, so that for-of can iterate over the result?
> --
> Dr. Axel Rauschmayer
> axel at rauschma.de
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130826/c55b5d1d/attachment.html>

More information about the es-discuss mailing list