I created an class with which it is possible to get all start and end positions of groups within a regexp object (https://github.com/valorize/MultiRegExp2). I want to wrap the initial regexp by this new "class" and add a new method execForAllGroups. How can I do this / overwrite the old regular expression but still use all its functions like search, test etc.?
I have:
function MultiRegExp2(baseRegExp) {
let filled = fillGroups(baseRegExp);
this.regexp = filled.regexp;
this.groupIndexMapper = filled.groupIndexMapper;
this.previousGroupsForGroup = filled.previousGroupsForGroup;
}
MultiRegExp2.prototype = new RegExp();
MultiRegExp2.prototype.execForAllGroups = function(string) {
let matches = RegExp.prototype.exec.call(this.regexp, string);
...
Edit: Thanks to T.J. Crowder I adapted the ES6 class syntax and extended RegExp:
class MultiRegExp extends RegExp {
yourNiftyMethod() {
console.log("This is your nifty method");
}
}
But
let rex = new MultiRegExp(); // rex.constructor.name is RegExp not MultiRegExp
rex.yourNiftyMethod(); // returns: rex.yourNiftyMethod is not a function
When I extend from String or another Object it all works as expected.