Letting RegExp method return something iterable?

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon Aug 26 18:48:25 PDT 2013


one lazy hilarious thought on that though ...


On Mon, Aug 26, 2013 at 5:30 PM, Forbes Lindesay <forbes at lindesay.co.uk>wrote:

>  `String#split` already is iterable because it returns an array.  What it
> isn't is **lazy**.
>
> <https://mail.mozilla.org/listinfo/es-discuss>
>
>
it's straight forward to make String#split(re) lazy using the `lastIndex`
indeed:

```javascript

function* lazySplit(str, re) {
  for (var
    i = 0;
    re.test(str);
    i = re.lastIndex
  )
    yield str.slice(
      i, re.lastIndex - RegExp.lastMatch.length
    )
  ;
  yield str.slice(i);
}

```

There,
    Best Regards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130826/f7a53881/attachment.html>


More information about the es-discuss mailing list