This seems a little derpy, but it seems that the browser is doing some type checking behind the scenes. This is my example that ran:
var Foo = function() {
this.toString = function() {
return "[object RegExp]";
}
return this;
};
Foo.prototype = Object.create(new RegExp);
var foo = new Foo("[aeiou]+", 'g' );
var match = 'The quickening'.match( foo );
console.log(match);
-- ["e", index: 2, input: "The quickening"]
So its odd, but yeah just making sure that the toString is a function defined on the instance and it seems to work just fine... This is of course tricking the browsers internal "is a RegExp" check... so no guarantees on working for all cases... for example:
foo.exec("The quickening")
dies with:
TypeError: Method RegExp.prototype.exec called on incompatible receiver [object Object]
So perhaps a better approach would be to make your object just proxy through to an internal RegExp object that it makes when it is constructed... you can proxy all the RegExp methods you want and then also add your own. Its a little derpy but you could probably get more of the methods to work as expected. We do something similar with a Capped Array implementation: https://github.com/jive/JiveJS-Commons/blob/master/libs/capped.js But then again Array behaves a lot nicer than RegExp does for this kind of thing.