JavaScript 字符串搜索

用于搜索字符串的 JavaScript 方法:

  • String.indexOf()
  • String.lastIndexOf()
  • String.startsWith()
  • String.endsWith()

String.indexOf()

indexOf() 方法返回指定文本在字符串中第一次出現(的位置)的索引:

實例

let str = "Please locate where 'locate' occurs!";
str.indexOf("locate")    // 返回 7

親自試一試

JavaScript 從零開始計算位置。

0 是字符串中的第一個位置,1 是第二個,2 是第三個 ......

String.lastIndexOf()

lastIndexOf() 方法返回指定文本在字符串中最后一次出現的索引:

實例

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate")    // 返回 21

親自試一試

如果未找到文本,indexOf()lastIndexOf() 都返回 -1:

實例

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("Bill")    // 返回 -1

親自試一試

這兩種方法都接受第二個參數作為搜索的開始位置:

實例

let str = "Please locate where 'locate' occurs!";
str.indexOf("locate", 15)    // 返回 21

親自試一試

lastIndexOf() 方法向后搜索(從末尾到開頭),意思是:如果第二個參數是 15,則從位置 15 開始搜索,一直搜索到字符串的開頭。

實例

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate", 15)    // 返回 7

親自試一試

String.search()

search() 方法在字符串中搜索指定值并返回匹配的位置:

實例

let str = "Please locate where 'locate' occurs!";
str.search("locate")     // 返回 7

親自試一試

您注意到了嗎?

indexOf()search() 這兩個方法,相等嗎?

它們接受相同的參數,并返回相同的值?

這兩種方法并不相等。差別如下:

  • search() 方法不能接受第二個起始位置參數。
  • indexOf() 方法不能采用強大的搜索值(正則表達式)。

您將在后面的章節中學到有關正則表達式的更多內容。

String.match()

match() 方法根據正則表達式在字符串中搜索匹配項,并將匹配項作為 Array 對象返回。

實例 1

在字符串中搜索 "ain":

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g)    // 返回數組 [ain,ain,ain]

親自試一試

請在 JS RegExp 一章中學習有關正則表達式的更多內容。

如果正則表達式不包含 g 修飾符(執行全局搜索),match() 方法將只返回字符串中的第一個匹配項。

語法

string.match(regexp)
regexp 必需。需要搜索的值,為正則表達式。
返回: 數組,包含匹配項,每個匹配項對應一個項目,若未找到匹配項,則為 null。

實例 2

對 "ain" 執行不區分大小寫的全局搜索:

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi)   // 返回數組 [ain,AIN,ain,ain]

親自試一試

String.includes()

如果字符串包含指定值,includes() 方法返回 true。

實例

let text = "Hello world, welcome to the universe.";
text.includes("world")    // 返回 true

親自試一試

瀏覽器支持

Internet Explorer 不支持 String.includes()。

Chrome IE Firefox Safari Opera
Chrome 41 Edge 12 Firefox 40 Safari 9 Opera 28
2015 年 3 月 2015 年 7 月 2015 年 8 月 2015 年 10 月 2015 年 3 月

語法

string.includes(searchvalue, start)
searchvalue 必需。需要搜索的字符串。
start 可選。默認為 0. 開始搜索的位置。
返回: 如果字符串包含該值則返回 true,否則返回 false
JS 版本: ES6 (2015)

檢查字符串是否包含 "world",從位置 12 開始搜索:

let text = "Hello world, welcome to the universe.";
text.includes("world", 12)    // 返回 false

親自試一試

String.startsWith()

如果字符串以指定值開頭,則 startsWith() 方法返回 true,否則返回 false

實例

let text = "Hello world, welcome to the universe.";
text.startsWith("Hello")   // 返回 true

親自試一試

語法

string.startsWith(searchvalue, start)

參數值

參數 描述
searchvalue 必需。需要搜索的值。
start 可選。默認為 0。開始搜索的位置。

實例

let text = "Hello world, welcome to the universe.";
text.startsWith("world")    // 返回 false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5)    // 返回 false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 6)    // 返回 true

親自試一試

注釋:startsWith() 方法區分大小寫。

Internet Explorer 不支持 startsWith() 方法。

第一個完全支持的瀏覽器版本是:

Chrome IE Firefox Safari Opera
Chrome 41 Edge 12 Firefox 17 Safari 9 Opera 28
2015 年 3 月 2015 年 7 月 2015 年 8 月 2015 年 10 月 2015 年 3 月

String.endsWith()

如果字符串以指定值結尾,則 endsWith() 方法返回 true,否則返回 false

實例

檢查字符串是否以 "Gates" 結尾:

var text = "Bill Gates";
text.endsWith("Gates")    // 返回 true

親自試一試

語法

string.endsWith(searchvalue, length)

參數值

參數 描述
searchvalue 必需。需要搜索的值。
length 可選。要搜索的長度。

檢索以 "world" 結尾的字符串的前 11 個字符:

let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11)    // 返回 true

親自試一試

注釋:endsWith() 方法區分大小寫。

Internet Explorer 不支持 endsWith() 方法。

第一個完全支持該方法的瀏覽器版本是:

Chrome IE Firefox Safari Opera
Chrome 51 Edge 15 Firefox 54 Safari 10 Opera 38
2016 年 5 月 2017 年 4 月 2017 年 6 月 2016 年 9 月 2016 年 6 月

完整字符串參考手冊

如需完整參考,請訪問我們的完整 JavaScript 字符串參考手冊

該手冊包含所有字符串屬性和方法的描述和實例。