String.prototype.indexOf()

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

indexOf() 方法用于在 String 值中搜索并返回指定子字符串第一次出现的索引。它接受一个可选的起始位置,并返回在指定位置及之后第一次出现的指定子字符串的索引。

试一试

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"

语法

js
indexOf(searchString)
indexOf(searchString, position)

参数

searchString

要搜索的子字符串。所有值都会被强制转换为字符串,因此省略它或传递 undefined 会导致 indexOf() 搜索字符串 "undefined",这通常不是你想要的。

position 可选

该方法返回在 position 及以上位置第一次出现的指定子字符串的索引,position 的默认值为 0。如果 position 大于调用字符串的长度,该方法将不会搜索调用字符串。如果 position 小于零,该方法将表现得如同 position0 一样。

  • 'hello world hello'.indexOf('o', -5) 返回 4 — 因为这会导致该方法表现得如同第二个参数为 0,而第一个大于或等于 0 的 'o' 出现在位置 4

  • 'hello world hello'.indexOf('world', 12) 返回 -1 — 因为,虽然子字符串 world 确实出现在索引 6 处,但该位置并不大于或等于 12

  • 'hello world hello'.indexOf('o', 99) 返回 -1 — 因为 99 大于 hello world hello 的长度,这导致该方法根本不搜索字符串。

返回值

找到的 searchString 第一次出现的索引,如果未找到则返回 -1

使用空搜索字符串时的返回值

搜索空字符串会产生奇怪的结果。如果没有第二个参数,或者第二个参数的值小于调用字符串的长度,返回值与第二个参数的值相同。

js
"hello world".indexOf(""); // returns 0
"hello world".indexOf("", 0); // returns 0
"hello world".indexOf("", 3); // returns 3
"hello world".indexOf("", 8); // returns 8

但是,当第二个参数的值大于或等于字符串的长度时,返回值是字符串的长度。

js
"hello world".indexOf("", 11); // returns 11
"hello world".indexOf("", 13); // returns 11
"hello world".indexOf("", 22); // returns 11

在前一种情况下,该方法表现得好像在第二个参数指定的位置之后立即找到了一个空字符串。在后一种情况下,该方法表现得好像在调用字符串的末尾找到了一个空字符串。

描述

字符串是零索引的:字符串第一个字符的索引是 0,字符串最后一个字符的索引是字符串长度减 1。

js
"Blue Whale".indexOf("Blue"); // returns  0
"Blue Whale".indexOf("Wale"); // returns -1
"Blue Whale".indexOf("Whale", 0); // returns  5
"Blue Whale".indexOf("Whale", 5); // returns  5
"Blue Whale".indexOf("Whale", 7); // returns -1
"Blue Whale".indexOf(""); // returns  0
"Blue Whale".indexOf("", 9); // returns  9
"Blue Whale".indexOf("", 10); // returns 10
"Blue Whale".indexOf("", 11); // returns 10

indexOf() 方法是区分大小写的。例如,以下表达式返回 -1

js
"Blue Whale".indexOf("blue"); // returns -1

检查出现次数

当检查某个特定子字符串是否出现在字符串中时,正确的检查方法是测试返回值是否为 -1

js
"Blue Whale".indexOf("Blue") !== -1; // true; found 'Blue' in 'Blue Whale'
"Blue Whale".indexOf("Wale") !== -1; // false; no 'Wale' in 'Blue Whale'

示例

使用 indexOf()

以下示例使用 indexOf() 在字符串 "Brave new world" 中查找子字符串。

js
const str = "Brave new world";

console.log(str.indexOf("w")); // 8
console.log(str.indexOf("new")); // 6

indexOf() 和区分大小写

以下示例定义了两个字符串变量。

这两个变量包含相同的字符串,只是第二个字符串包含大写字母。第一个 console.log() 方法显示 19。但是因为 indexOf() 方法是区分大小写的,所以字符串 "cheddar"myCapString 中找不到,因此第二个 console.log() 方法显示 -1

js
const myString = "brie, pepper jack, cheddar";
const myCapString = "Brie, Pepper Jack, Cheddar";

console.log(myString.indexOf("cheddar")); // 19
console.log(myCapString.indexOf("cheddar")); // -1

使用 indexOf() 计算字符串中某个字母的出现次数

以下示例将 count 设置为字符串 str 中字母 e 的出现次数。

js
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

规范

规范
ECMAScript® 2026 语言规范
# sec-string.prototype.indexof

浏览器兼容性

另见