I have the following code to filter a list based on what the user types in an input field:
const searchQuery = e.target.value.trim();
items
.filter((item) =>
item.title.match(new RegExp(searchQuery, 'gi'))
)
.map(item => {
// Do something...
});
it works great except everything breaks when the user enters some funky text in the input field like:
//on\/: \/: \
Results in the following error:
SyntaxError: Invalid regular expression: ///on\/: \/: \/: \ at end of pattern
How to approach this correctly so that my code doesn't break based on user input?