RegExps that don't modify global state?

Andy Earnshaw andyearnshaw at gmail.com
Tue Sep 23 05:58:40 PDT 2014


I've found the functionality useful on occasion too, but I've also seen it
misused.  I could see the extra flag been used by libraries everywhere,
Intl.js for instance runs a "restoring" regular expression to restore the
state of the RegExp object after some internal regexes are used (it does
this in order to pass many tests in the test262/intl402 suite) – a simple
flag to disable the behaviour would have been great at the time.

On Wed, Sep 17, 2014 at 6:13 AM, Jussi Kalliokoski <
jussi.kalliokoski at gmail.com> wrote:

> I personally find this functionality very useful and would be saddened if
> /u which I want to use all of the sudden broke this feature.
>

What about properly speccing the functionality on the RegExp instance
itself? e.g. on a `matches` or `lastMatches` property:

    var re = /foo:(\d+)/;
    if (re.test(str) && parseInt(re.lastMatches[1], 10) > 15) {
        ...
    }

This would be a way of avoiding using assignment in the conditional
expression if you don't want to disable that option in your linter.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140923/abd74ff8/attachment.html>


More information about the es-discuss mailing list