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

瀏覽器相容性

另見