Closed cpcallen closed 3 years ago
Strange that it's inconsistent in JavaScript:
var re = /foo/;
alert(re === RegExp(re)); // true
var str = new String('hello');
alert(str === String(str)); // false
Strange that it's inconsistent in JavaScript:
var re = /foo/; alert(re === RegExp(re)); // true
But re !== new RegExp(re)
.
I agree it's kind of warty, but it sort of makes sense if you think of RegExp(r)
and String(s)
as casts to RegExp and string (not String, unfortunately), respectively, such that if r
is already a RegExp then there is nothing to do—whereas new RegExp(r)
and new String(s)
give you a new object of the corresponding type.
(There's no excuse for Date(d)
, though. That's just wrong no matter how you look at it.)
There are a few ways in which the
RegExp
constructor's behaviour diverges from the spec (and V8):