Case-insensitive search

I’m trying to get a case-insensitive search with two strings in JavaScript working.

Normally it would be like this:

var string="Stackoverflow is the BEST";
var result= string.search(/best/i);
alert(result);

The /i flag would be for case-insensitive.

But I need to search for a second string; without the flag it works perfect:

var string="Stackoverflow is the BEST";
var searchstring="best";
var result= string.search(searchstring);
alert(result);

If I add the /i flag to the above example it would search for searchstring and not for what is in the variable “searchstring” (next example not working):

var string="Stackoverflow is the BEST";
var searchstring="best";
var result= string.search(/searchstring/i);
alert(result);

How can I achieve this?

12 Answers
12

Leave a Comment