0

I would like to create function which filter items in an array for a specific value passed in.

Currently I am using template literal to pass the value for RegEx to its constructor, but with no success.

What I am doing wrong in my code?

The result should be an array filtered (in this example with 2 items).

let data = ['Gone With the Wind', 'Star Wars: Episode IV', 'Jaws', 'Matrix', 'Matrix backstage', 'Star Wars: Episode I', 'WindWind'  ];


let filter = (data, value) =>{
    //return data.filter(s => s.match(/value/i))
    return data.filter(s => s.match(`/${value}/i`))
};

console.log(filter(data, 'Wind'));
Radex
  • 7,815
  • 23
  • 54
  • 86

1 Answers1

1
new RegExp(value, 'i');

RegExp documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

Magus
  • 14,796
  • 3
  • 36
  • 51