Home > Back-end >  How to props array of regex decorators of dynamic strings? Javascript
How to props array of regex decorators of dynamic strings? Javascript

Time:12-17

I need to props few dynamic words regex inside child component with regex. This is work:

In this situation my regex work but... I need to regex some dynamic data which I don't know which is word is. All data is in my array. What I am try?

CodePudding user response:

Use the RegExp class constructor for your dynamic regexp:

 const decorators = [ 
    arrayOfWord.map((data) => {
        // data is a string value:
        // exp: data = Garnosny 
        // new RegExp(data, 'g') = /Garnosny/g
        return { regex: new RegExp(data, 'g'), className: 'some css class' } 
    }
  ];

CodePudding user response:

We have a couple of issues here...

1- In the second code snippet, you are missing a return statement in the arrow function passed to "map()".

2- In the third code snippet, you are calling "map()" again on the "data" array. But there is not clear what you are trying to do.

If you can explain a little bit better sure we can help.

  • Related