case-insensitive JavaScript indexOf

Source: Internet
Author: User
javascript| case

Look at me, it's compatible with the original indexOf.

<script language=javascript>
/*
Author: Niu (New Kunliang) qq:273352165 msn:niukl@hotmail.com

Disclaimer: You can use it for free, please keep this information
If you have anything to mend, please 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>


The following is the program code <script language=javascript>
/*
Author: Niu (New Kunliang) qq:273352165 msn:niukl@hotmail.com

Disclaimer: You can use it for free, please keep this information
If you have anything to mend, please 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>
2005-11-7 16:20 Chrothra
The following is the program code
<script>
String.prototype.indexOf = function (f,s,m) {
if (f = = null) {return;}
var st = (s!= null && typeof (s) = = "Number" && s < this.length)? parseint (s): 0;
var mm = (M = = False)? "I": "";
var re = eval ("/" + f.tostring () + "/" +mm);
var ss = (St = = 0)? This:this.substr (ST);
var RT = Ss.match (re);
return (RT = null)? -1:rt.index + st;
}
</script>



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.