Home > Web Front-end > JS Tutorial > body text

Instructions for using the javascript indexOf function_Basic knowledge

WBOY
Release: 2016-05-16 19:02:59
Original
1234 people have browsed it

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

Program code

where strObj is a required option. String object or text.
str is required. The substring to find in the String object.
startIndex is optional. This integer value indicates the position within the String object to start searching, starting at 0. If omitted, the search is from the beginning of the string.


Note: indexOf for JavaScript is case-sensitive.

The indexOf function method in JavaScript returns an integer value indicating the starting position of the substring within the String object. If the string is not found, -1 is returned. If startindex is negative, startindex is treated as zero. If it is greater than the largest character position index, it is treated as the largest possible index.

The indexOf function performs searches from left to right.
The following example illustrates the usage of the indexOf function method.

Program code

var str1="fdiejDIFADF";
var str="e";
var i=str1.indexOf(str);
alert(i );



As mentioned before, indexOf is case-sensitive. Sometimes this causes us some trouble, so how to solve it? ? Of course, the easiest way is to convert the characters to uppercase or lowercase using toLowerCase or toUpperCase.
The code is as follows:

Program code






The following method uses regular expressions to extend indexOf (from the Internet)

Program Code






The following extension is more powerful. It is compatible with the original indexOf function and can also ignore the size. Find (also from the web).

Program code
<script> <BR>var Str = 'ABCDEF'; <BR>var Str1 = 'bcd'; <BR>alert(Str.toLowerCase().indexOf(Str1.toLowerCase())); <BR>str2 = 'AbCdEf'; <BR>alert(Str2.toLowerCase().indexOf(Str1.toLowerCase())); <BR></script><script> <BR>String.prototype.indexOf = function(f,m){ <BR>var mm = (m == false) ? "i":""; <BR>var re = eval("/"+ f +"/"+mm); <BR>var rt = this.match(re); <BR>return (rt == null) ? -1:rt.index; <BR>} <BR>var test = "absnegKIugfkalg"; <BR>alert(test.indexOf("kiu",false)); <BR></script> <script> <BR>String.prototype._indexOf = String.prototype.indexOf; <BR>String.prototype.indexOf = function() <BR>{ <BR> if(typeof(arguments[arguments.length - 1]) != 'boolean') <BR> return this._indexOf.apply(this,arguments); <BR> else <BR> { <BR> var bi = arguments[arguments.length - 1]; <BR> var thisObj = this; <BR> var idx = 0; <BR> if(typeof(arguments[arguments.length - 2]) == 'number') <BR> { <BR> idx = arguments[arguments.length - 2]; <BR> thisObj = this.substr(idx); <BR> } <br><br> var re = new RegExp(arguments[0],bi?'i':''); <BR> var r = thisObj.match(re); <BR> return r==null?-1:r.index + idx; <BR> } <BR>} <BR>alert("bcssssasdfsdf".indexOf('A',3,true)); <BR>alert("bcssssasdfsdf".indexOf('a',3)); <BR></script>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template