I have some code that looks like this:
// searchTerm is input by the user
// I am escaping especial characters as described here https://stackoverflow.com/a/6969486
let searchTerm = '1054';
var regex = new RegExp(searchTerm, 'gi');
let data = [
{ id: '10545' },
{ id: '10544' },
{ id: '10543' },
{ id: '10542' },
{ id: '10541' }
];
let result = data.filter(
row => regex.test(row.id)
);
console.log(result);
And here is the result in the console:
[
{ id:"10545" },
{ id:"10543" },
{ id:"10541" }
]
I was expecting it to match all five IDs. Why does it match only three? How can I make it match all five?
Thank you.