Andrea Giammarchi (2014-07-16T07:31:38.000Z)
expected by specs ... try this

r = /\u0020+$/g; p = r.exec("  "); r.lastIndex = 0; q = r.exec("  ");
alert(JSON.stringify([p, q]))

and you are good to go

... without resetting lastIndex, you can also use "  ".replace(r,
'whatever') and it's going to work every time.

Have a look at these slides to know more about JS RegExp

https://github.com/WebReflection/talks/tree/master/BerlinJS



On Wed, Jul 16, 2014 at 12:26 AM, Alex Vincent <ajvincent at gmail.com> wrote:

> r = /\u0020+$/g; p = r.exec("  "); q = r.exec("  "); JSON.stringify([p, q])
> // "[[\"  \"],null]"
>
> Why does calling exec the second time generate null?  When I try the
> regular expression without the /g flag, I get:
> // "[[\"  \"],[\"  \"]]"
>
>
>
> --
> "The first step in confirming there is a bug in someone else's work is
> confirming there are no bugs in your own."
> -- Alexander J. Vincent, June 30, 2001
>
> _______________________________________________
> 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/20140716/8779fd01/attachment.html>
forbes at lindesay.co.uk (2014-07-16T22:35:44.823Z)
expected by specs ... try this

```js
r = /\u0020+$/g; p = r.exec("  "); r.lastIndex = 0; q = r.exec("  ");
alert(JSON.stringify([p, q]))
```

and you are good to go

... without resetting lastIndex, you can also use `"  ".replace(r, 'whatever')` and it's going to work every time.

Have a look at these slides to know more about JS RegExp

https://github.com/WebReflection/talks/tree/master/BerlinJS