IndexOf is strictly case-sensitive in JavaScript
How can you ignore the case?
Change them all to uppercase or lowercase, and compare them.
IndexOf the character position of the substring for the first time within the object
With toLowerCase or toUpperCase
<script>
var testStr = 'ABCDEF';
var subStr = 'bcd';
alert(testStr.toLowerCase().indexOf(subStr.toLowerCase()));
testStr = 'AbCdEf';
alert(testStr.toLowerCase().indexOf(subStr.toLowerCase()));
</script>
Or:
<script>
String.prototype.indexOf = function(f,m){
var mm = (m == false) ? "i":"";
var re = eval("/"+ f +"/"+mm);
var rt = this.match(re);
return (rt == null) ? -1:rt.index;
}
var test = "absnegKIugfkalg";
alert(test.indexOf("kiu",false));
</script>
Or:
Look at me, it's compatible with the original indexOf.
<script language= "javascript"
/*
Author: niu (New Kunliang) qq:273352165 msn:niukl@hotmail.com
Declaration: Free to use, please keep this information
if you have any changed, remember to tell me!
*/
String.prototype._indexof = String.prototype.indexOf;
String.prototype.indexOf = function ()
{
if (typeof (Arguments[arguments.length-1))!= ' Boolean '
Return this._indexof.apply (this,arguments);
Else
{
var bi = arguments[arguments.length-1];
var thisobj = this;
var idx = 0;
if (typeof (Arguments[arguments.length-2]) = = ' number ')
{
idx = arguments[arguments. LENGTH-2];
Thisobj = this.substr (idx);
}
var re = new RegExp (arguments[0],bi? ') I ': ');
var r = Thisobj.match (re);
return r==null?-1:r.index + idx;
}
alert ("Bcssssasdfsdf". IndexOf (' A ', 3,true));
Alert ("Bcssssasdfsdf". IndexOf (' A ', 3));
</script>