RegExps that don't modify global state?

Viktor Mukhachev esdiscusser at yandex.ru
Tue Sep 23 23:36:03 PDT 2014


>Another idea is to to define a variant of the RegExp.prototype.exec() method, that does the Right Thing (doesn't read/write stuff on the RegExp instance, nor on the RegExp global, nor I don't know where):
>
>RegExp.prototype.run (str, params):
>
>    Do what is currently specified for `RegExp.prototype.exec`, except that:
>
>        * global, sticky and lastIndex properties are read and written on `params` instead of `this`
>        * implementations are not allowed to extend that method in order to mess with `RegExp`, etc.
>All other (legacy) methods are rewritten in terms of RE.p.run (in the current ES6 draft, they are mostly written in terms of RE.p.exec).

RegExp.prototype.exec returns array with extra properties (input, index), may be it is better to return something other for `run`, frozen value object with `0`, `1`, ...  keys for example. What do you think?

Actually, `index` and "input" are not very interesting, as input is a string passed to `exec` and index = string.indexOf(match[0], lastIndex), right?


More information about the es-discuss mailing list