They are deprecated because it an API that is hard to use correctly and leaks information. When you do a match on a regex, generally you expect a value to be returned by the matching function, or at least tracked on the RegExp
instance itself. To be clear, these are properties of RegExp
, not of instances. Take this example:
var re = /([0-9])([0-9])([0-9])/;
re.test("345");
var three = RegExp.$1;
var four = RegExp.$2;
var five = RegExp.$3;
that is a seriously ugly API.
Not only is it hard to use, but it means that if you are writing a library, things could access internal state of your API. There are other dangers in this example, but consider a case like:
matchPrivateKey();
var private = RegExp.$1;
If inside matchSecretKey
you used a regex to match some private key, it is now accessible outside of your module and leaked to other code running on the page.
It is much nicer to use an API like
var re = /([0-9])([0-9])([0-9])/;
var [, three, four, five] = "345".match(re);
where .match
returns an array with the matched results.
There is never a time where you'd expect an object instance to mutate properties on the constructor function.
"The following properties are deprecated. This does not affect their use in replacement strings."
Doesn't that sound self-contradictory?
Not at all. The properties on RegExp
are deprecated, but doing "345".replace(/3([0-9])5/, '$1')
is still totally find since a string pattern is unrelated to the constructor property.