此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
String.prototype.indexOf()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
String 的indexOf() 方法在字符串中搜索指定子字符串,并返回其第一次出现的位置索引。它可以接受一个可选的参数指定搜索的起始位置,如果找到了指定的子字符串,则返回的位置索引大于或等于指定的数字。
In this article
尝试一下
const paragraph = "I think Ruth's dog is cuter than your dog!";const searchTerm = "dog";const indexOfFirst = paragraph.indexOf(searchTerm);console.log(`The index of the first "${searchTerm}" is ${indexOfFirst}`);// Expected output: "The index of the first "dog" is 15"console.log( `The index of the second "${searchTerm}" is ${paragraph.indexOf( searchTerm, indexOfFirst + 1, )}`,);// Expected output: "The index of the second "dog" is 38"语法
indexOf(searchString)indexOf(searchString, position)参数
searchValue要搜索的子字符串。所有传入值都会被强制转换为字符串,因此如果该参数被省略或传入
undefined,indexOf()方法会在字符串中搜索"undefined",这通常不是你想要的。position可选该方法返回指定子字符串在大于或等于
position位置的第一次出现的索引,默认为0。如果position大于调用字符串的长度,则该方法根本不搜索调用字符串。如果position小于零,该方法的行为就像position为0时一样。hello world hello'.indexOf('o', -5)返回4——因为它使该方法的行为类似于第二个参数为0,并且o在大于或等于0位置的第一次出现是在4位置。'hello world hello'.indexOf('world', 12)返回-1——因为,虽然子字符串world确实出现在索引6处,但该位置不大于或等于12。'hello world hello'.indexOf('o', 99)返回-1——因为99大于hello world hello的长度,这会导致方法根本不搜索字符串。
返回值
查找的字符串searchValue 的第一次出现的索引,如果没有找到,则返回-1。
搜索空字符串时的返回值
搜索空字符串会产生奇怪的结果。如果没有第二个参数,或者第二个参数的值小于调用字符串的长度,则返回值与第二个参数的值相同:
"hello world".indexOf(""); // 返回 0"hello world".indexOf("", 0); // 返回 0"hello world".indexOf("", 3); // 返回 3"hello world".indexOf("", 8); // 返回 8但是,如果第二个参数的值大于或等于字符串的长度,则返回值是字符串的长度:
"hello world".indexOf("", 11); // 返回 11"hello world".indexOf("", 13); // 返回 11"hello world".indexOf("", 22); // 返回 11在前一个实例中,该方法的行为就像在第二个参数指定的位置之后发现了一个空字符串。在后一个实例中,该方法的行为就好像在调用字符串的末尾找到了一个空字符串。
描述
字符串的索引从零开始:字符串的第一个字符的索引为0,字符串的最后一个字符的索引为字符串长度减 1。
"Blue Whale".indexOf("Blue"); // 返回 0"Blue Whale".indexOf("Blute"); // 返回 -1"Blue Whale".indexOf("Whale", 0); // 返回 5"Blue Whale".indexOf("Whale", 5); // 返回 5"Blue Whale".indexOf("Whale", 7); // 返回 -1"Blue Whale".indexOf(""); // 返回 0"Blue Whale".indexOf("", 9); // 返回 9"Blue Whale".indexOf("", 10); // 返回 10"Blue Whale".indexOf("", 11); // 返回 10indexOf() 方法是区分大小写的。例如,下面的表达式将返回-1:
"Blue Whale".indexOf("blue"); // 返回 -1检测指定字符串是否存在
当检查字符串中是否出现特定的子字符串时,正确的检查方法是测试返回值是否为-1:
"Blue Whale".indexOf("Blue") !== -1; // true;在 'Blue Whale' 中找到 'Blue'"Blue Whale".indexOf("Bloe") !== -1; // false;'Blue Whale' 中不存在 'Bloe'示例
>使用 indexOf()
下面的例子使用indexOf() 来定位字符串"Brave new world" 中的子字符串。
const str = "Brave new world";console.log(str.indexOf("w")); // 8console.log(str.indexOf("new")); // 6indexOf() 和区分大小写
下例定义了两个字符串变量。
两个变量包含相同的字符串,只是第二个字符串中的某些字符为大写。第一个console.log() 方法输出19。但是由于indexOf() 方法区分大小写,因此不会在myCapString 中发现字符串“cheddar",所以第二个console.log() 方法会输出-1。
const myString = "brie, pepper jack, cheddar";const myCapString = "Brie, Pepper Jack, Cheddar";console.log(myString.indexOf("cheddar")); // 19console.log(myCapString.indexOf("cheddar")); // -1使用 indexOf() 统计一个字符串中某个字母出现的次数
在下例中,使用count 来记录字母e 在字符串str 中出现的次数:
const str = "To be, or not to be, that is the question.";let count = 0;let position = str.indexOf("e");while (position !== -1) { count++; position = str.indexOf("e", position + 1);}console.log(count); // 4规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-string.prototype.indexof> |