Regular expression in a component computed

Hello, forum.
I was trying to use matchAll in one of a component’s computed methods. This didn’t work, I keep on getting the following error:
[Vue warn]: Error in render: "TypeError: matchAll must be called with a global RegExp"
The regular expression itself is in the data object of the component. Where should I put it so that it works?
P.S. This is hos I call it:

    var matches = pocket0.matchAll(this.re);
          for (const match of matches) {
            console.log(match);
            console.log(match.index);
          }

And this is how re is defined:

    "data"() {
          return {
            re: new RegExp('(?=ATOM)(.*)'),
          }
        },

Can you show your full computed?

Also, is there a reason you’re placing the re within data? It doesn’t look like you need it to be reactive, so just declare it as a constant outside of the component instance.

The RegExp object needs the have it’s global option set.

re: new RegExp(’(?=ATOM)(.*)’,‘g’),

1 Like