JavaScriptindexOf case-insensitive javascript skills

Source: Internet
Author: User
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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.