<p>I'm trying to get a case-insensitive search with two strings in JavaScript working.</p>

<p>Normally it would be like this:</p>

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

<p>The <code>/i</code> flag would be for case-insensitive.</p>

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

<pre><code>var string="Stackoverflow is the BEST";
var searchstring="best";
var result= string.search(searchstring);
alert(result);
</code></pre>

<p>If I add the <code>/i</code> flag to the above example it would search for searchstring and not for what is in the variable "searchstring" (next example not working):</p>

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

<p>How can I achieve this?</p>

Yeah, use
```
.match
```
, rather than
```
.search
```
. The result from the
```
.match
```
call will return the actual string that was matched itself, but it can still be used as a boolean value.
```
```
var string = "Stackoverflow is the BEST";
var result = string.match(/best/i);
// result == 'BEST';

if (result){
    alert('Matched');
}
```
```
Using a regular expression like that is probably the tidiest and most obvious way to do that in JavaScript, but bear in mind it
is
a regular expression, and thus can contain regex metacharacters. If you want to take the string from elsewhere (eg, user input), or if you want to avoid having to escape a lot of metacharacters, then you're probably best using
```
indexOf
```
like this:
```
```
matchString = 'best';
// If the match string is coming from user input you could do
// matchString = userInput.toLowerCase() here.

if (string.toLowerCase().indexOf(matchString) != -1){
    alert('Matched');
}
```
```