JavaScript indexof Ignore case _javascript tips

Source: Internet
Author: User
Here's how to get the IndexOf function in JavaScript to ignore the case.
Change them all to uppercase or lowercase, and compare them.
This is a relatively simple approach!
IndexOf the character position of the substring for the first time within the object.
Use toLowerCase or toUpperCase code as follows:
<script> var teststr = ' ABCDEF '; var subStr = ' BCD '; Alert (Teststr.tolowercase (). IndexOf (Substr.tolowercase ())); Teststr = ' AbCdEf '; Alert (Teststr.tolowercase (). IndexOf (Substr.tolowercase ())); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Or look at how the following is extended for the IndexOf function:
<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 All SELECT Note: If the need to introduce external JS need to refresh to perform]

The following compatible original IndexOf function allows you to add indexof functions that are directly used to ignore the case of JavaScript.
<script language= "JavaScript" > String.prototype._indexof = String.prototype.indexOf; String.prototype.indexOf = function () {if (typeof (Arguments[arguments.length-1))!= ' Boolean ' return THIS._INDEXOF.A Pply (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 All selected note: If you need to introduce external JS need to refresh to perform]
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.