0

Sorry if the question already get answered somewhere, but i dont find anything for this specific issue online.

I'm trying to create an array of RegExp and apply them after to filter an array. But I can pop and use my regex directly in the anonymous filter function.

As you can see bellow it only dont work in this specific configuration. (I don't try with other object than RegExp yet).

var regex = [new RegExp("01$"), new RegExp("01$")];
  var arr = ["001","02","012", "12301"];
  var take = regex.pop();
  //work
  console.log(arr.filter( a=> a.match(take)));
  console.log(arr.filter( a=> a.match(new RegExp("01$"))));
  console.log(arr.filter( a=> a.match(regex[0])));
  
  //don't
  console.log(regex)
  console.log(arr.filter( a=> a.match(regex.pop())));
  console.log(regex)

Before I continue to test and undertand this behaviour i would like your inputs to know if i miss something obvious.

Thanks in advance

Julien Maret
  • 579
  • 1
  • 4
  • 22

1 Answers1

0

Assume you want to check each item matches any of the regular expressions, you can do this:

const matches = arr.filter(a => regex.some(r => r.test(a)));
Evan Trimboli
  • 29,900
  • 6
  • 45
  • 66