1

Possible Duplicate:
Javascript indexOf case insensitive

how to use IndexOf in javascript to search a key in a string without taking in mind if the key is in lower case or upper case

<script type="text/javascript">
 var s = "hello world";
 var key="HELLO";
 // the alert should say found if key is lower case or upper case     
 if(s.indexof(key)>1) alert("found") 
 </script>

Is their any function in javascript that can check if the key is lower or upper case

Community
  • 1
  • 1
Sora
  • 2,697
  • 15
  • 62
  • 127
  • `s.indexof(key)>1` => if key is at the beginning of s, `s.indexof(key)` will return 0. so you should have a -1 instead: `s.indexof(key)>-1` – Vince Aug 29 '12 at 13:19
  • let s = "hello world"; let key = "HELLO"; if(s.toLowerCase().indexOf(key.toLowerCase()) > -1){ Console.log("found"); } – Kalaivanan Jun 05 '18 at 06:47

4 Answers4

3

Convert both s and key to lower case using .toLowerCase() before using indexOf()

PhonicUK
  • 12,412
  • 3
  • 36
  • 61
0

look this:

check this

this will explain how it works

Aravindhan
  • 3,430
  • 7
  • 23
  • 40
  • Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Manse Aug 29 '12 at 13:11
0

Try the toLowerCase() function and also check if it is on the first position:

<script type="text/javascript">
var s = "hello world";
var key="HELLO";    
if(s.toLowerCase().indexof(key.toLowerCase())>=0) alert("found")
</script>
rekire
  • 45,039
  • 29
  • 149
  • 249
0

You could use case-insensitive matching:

var string = "This is a HelLo WoRld string";
var lookup = "hellO wOrld";

var result = string.match(lookup,"i");     // "i" = case insensitive
alert(result.index);                       // index starts at 0
vol7ron
  • 35,981
  • 19
  • 104
  • 164