Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript indexOf to ignore Case

Tags:

javascript

I am trying to find if an image has in its source name noPic which can be in upper or lower case.

var noPic = largeSrc.indexOf("nopic"); 

Should I write :

var noPic = largeSrc.toLowerCase().indexOf("nopic"); 

But this solution doesn't work...

like image 386
adardesign Avatar asked Aug 07 '09 17:08

adardesign


People also ask

How do you ignore cases in indexOf?

No, there is no case-insensitive way to call that function.

Is indexOf case insensitive?

Yes, the String. indexOf() methods are all case-sensitive.

Is indexOf case sensitive in Java?

The indexOf() method returns the index number where the target string is first found or -1 if the target is not found. Like equals(), the indexOf() method is case-sensitive, so uppercase and lowercase chars are considered to be different.


2 Answers

You can use regex with a case-insensitive modifier - admittedly not necessarily as fast as indexOf.

var noPic = largeSrc.search(/nopic/i); 
like image 181
jharlap Avatar answered Sep 18 '22 15:09

jharlap


No, there is no case-insensitive way to call that function. Perhaps the reason your second example doesn't work is because you are missing a call to the text() function.

Try this:

var search = "nopic"; var noPic = largeSrc.text().toLowerCase().indexOf(search.toLowerCase()); 
like image 25
Andrew Hare Avatar answered Sep 20 '22 15:09

Andrew Hare