I’ve used indexOf
to search for a term
within a text
(both are strings), like so:
if (text.indexOf(term) > -1) { …
However, I would like the search to not be case sensitive. I’ve tested with performing .toLowerCase()
on both strings, but the performance penalty of doing that is significant (I’m searching on an array of text
s with thousands of elements). I’m therefore considering switching to .search()
, like so:
var re = new RegExp(term, 'i');
// and then
if (text.search(re) > -1) { …
Is this approach feasible? How can I make sure that the term
is treated “literally” in this scenario, i.e. that certain characters in term
aren’t interpreted as special characters?