Javascript indexOf case-insensitive method_javascript skills

Source: Internet
Author: User
In javascript, how can I ignore the case sensitivity of indexOf? Replace all of them with uppercase or lowercase letters, and then compare them.
The first occurrence of a substring In the indexOf object.
Use 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]


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]


Look at me, compatible with the original indexOf <script language = "javascript">/* Author: A niu (NIU kunliang) QQ: 273352165 MSN: niukl@hotmail.com statement: Can Be Used for free, please keep this information. If you have made any changes, please 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
[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.