I want to create my own subclass of RegExp with additional methods. This is the most simplified version of my approach:
function subRegExp(){}
subRegExp.prototype = new RegExp();
regex = new subRegExp('[a-z]', 'g');
But I can not create a new instance.
It is reported that ECMAScript does not support subclassing of its own objects, but 5 years have passed, so I hope there are some options now.
How can i achieve this?
EDIT: is this normal or am I going to run into some problems?
function subRegExp(str, flags){
var instance = new RegExp(str, flags);
instance.setFlags = function(flags){
return new subRegExp(this.source, flags);
}
return instance;
}
regex = new subRegExp('[a-z]', 'g');
source
share