Letting RegExp method return something iterable?

Forbes Lindesay forbes at lindesay.co.uk
Wed Aug 28 02:12:45 PDT 2013


Right, my impression is that most of us are in agreement that it would be extremely useful to have a simple way to loop over the list of matches for a regular expression and do something with each one.  I don't see why @andrea doesn't see this need (maybe it's not something he's found need to do recently).

I think to move on, it would be useful to consider whether the method should return an array (which would be iterable and also have methods like `.map` built in) or a custom, lazy iterable (which might be better for efficiency if that laziness were useful, but will have disadvantages like lacking the array prototype methods and _presumably_ failing if you try to loop over it twice).

I'm guessing that code like:

```js
var matches = /foo/.execMultipleLazy('str')
for (let match of matches) {
  //do something
}
for (let match of matches) {
  //do something
}
for (let match of matches) {
  //do something
}
```

Would go wrong somehow whereas:

```js
var matches = /foo/.execMultipleGreedy('str')
for (let match of matches) {
  //do something
}
for (let match of matches) {
  //do something
}
for (let match of matches) {
  //do something
}
```

Would work fine?


More information about the es-discuss mailing list