3

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.

1 Answer 1

5

You have at least a couple of options. As I can see you're using ES2015 (aka ES6) features, the most obvious thing to do is to extend RegExp:

class MultiRegExp2 extends RegExp {
  yourNiftyMethod() {
    console.log("This is your nifty method");
  }
}

let rex = new MultiRegExp2(/\w+/); // or   = new MultiRegExp2("\\w+");
console.log(rex.test("testing"));  // "true"
rex.yourNiftyMethod();             // "This is your nifty method"

Alternately you could augment the built-in RegExp type by simply adding to RegExp.prototype:

RegExp.prototype.yourNiftyMethod = function() {
  console.log("This is your nifty method");
};

let rex = /\w+/;
console.log(rex.test("testing"));  // "true"
rex.yourNiftyMethod();             // "This is your nifty method"

Note that extending built-in prototypes is controversial, there are at least two camps, one saying "Never do that, you'll run into trouble" and another saying "This is what prototypes are for." From a pragmatic perspective, beware of naming conflicts — with other code also extending native prototypes, and with future additions to the base type as the language and its runtime evolve.

Sign up to request clarification or add additional context in comments.

3 Comments

I like the idea of extending RegExp with a class but I get: Uncaught TypeError: rex.yourNiftyMethod is not a function
Given the article v8project.blogspot.de/2017/01/… Maybe it is better to just save the regexp as an property and create hand-trough methods? test(str) { return this.regex.test(str); }?
@velop: Perhaps, or perhaps that's premature micro-optimization. :-) Interesting article, though. If hyper-performance is necessary, once Chrome 57 comes out, you might want to test it both ways. (This page suggests Chrome Canary may already be using that version of V8.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.