1

I'd like to make .search() case insensitive for a variable I've entered into it. From the documentation on W3 it appears I can only hard code case insensitive searches. The example on W3 goes:

var str = "Mr. Blue has a blue house";
var n = str.search(/blue/i);

My code looks like:

var searchTerm = $("input").val();
var source = $("#page5").html();
var found = source.search(searchTerm);

If I were to code it like the W3 example then the .search() command would look like:

var found = source.search(/searchTerm/i);

However, when I do that it appears to attempt to search for the literal text of "searchTerm" instead of the value within the variable searchTerm. Is there a way to use the case insensitivity on the search method while inserting a variable into it?

plumsmugler
  • 101
  • 1
  • 6
  • 1
    `source.toLowerCase().search(searchTerm.toLowerCase());` is the fast way, otherwise you're going to have to make a dynamic RegExp to specify an `/i` flag, which is complicated and slow to execute, especially if the user can search for reserved chars. – dandavis Mar 31 '16 at 20:23
  • 1
    http://stackoverflow.com/questions/177719/javascript-case-insensitive-search – Nikki9696 Mar 31 '16 at 20:26

1 Answers1

0

untested:

var searchTerm = $("input").val().toLowerCase(); 
var source = $("#page5").html().toLowerCase();
Jan Viehweger
  • 462
  • 5
  • 17
  • Problem is there are capitalized versions of the search word in the document I'm searching that I want to get access to. I want the program to get hits whenever it finds any version of the word. If the user enters "office" into the search input I want to find instances of both "office" and "Office" in the document I'm searching. – plumsmugler Mar 31 '16 at 21:04