Javascript indexOf function instructions

Source: Internet
Author: User

Usage: strObj. indexOf (str, startIndex [Optional])

Program code

StrObj is required. String object or text.
Str is required. The substring to be searched in the String object.
StartIndex is optional. This integer indicates the position where the String object starts searching, starting from 0. If it is omitted, It is searched from the beginning of the string.

Note: JavaScript indexOf is case sensitive.

The indexOf Function Method in JavaScript returns an integer indicating the starting position of the substring in the String object. If no string is found,-1 is returned. If startindex is negative, startindex is treated as zero. If it is larger than the index at the largest character location, it is regarded as the largest possible index.

The indexOf function performs search from left to right.
The following example illustrates how to use the indexOf function method.

Program code

Var str1 = "fdiejDIFADF ";
Var str = "e ";
Var I = str1.indexOf (str );
Alert (I );

As mentioned above, indexOf is a write with different sizes. Sometimes this causes us some trouble. How can we solve this problem ?? Of course, the simplest way is to convert the characters into uppercase or lowercase letters using toLowerCase or toUpperCase.
The Code is as follows:

Program code

<Script>
Var Str = 'abcdef ';
Var Str1 = 'bcd ';
Alert (Str. toLowerCase (). indexOf (Str1.toLowerCase ()));
Str2 = 'abcdef ';
Alert (Str2.toLowerCase (). indexOf (Str1.toLowerCase ()));
</Script>

The following method uses the regular expression to expand indexOf (from the Network)

Program code

<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>

The following extension is more powerful. It is compatible with the original indexOf function and can also be used to look up the ignore size (also from the network ).

Program code

<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 ("bcssasdfsdf". indexOf ('A', 3, true ));
Alert ("bcssasdfsdf". indexOf ('A', 3 ));
</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.