In javascript, the indexOf function is case-sensitive, that is, when we use the javascript indexOf function, it cannot be case-sensitive at the same time. This makes some of our string operations difficult. The following describes how to make the indexOf function in javascript case-insensitive.
Replace all of them with uppercase or lowercase letters, and then compare them.
This is a simple practice!
The first occurrence of a substring In the indexOf object,
The following code uses 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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
You can also see how the indexof function is extended below:
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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The following is compatible with the original indexOf function. You can add the indexOf function that ignores the case.
<Script language = "javascript"> 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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]