選單
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

JS 教程

JS HOME JS Introduction JS Where To JS Output JS Statements JS Syntax JS Comments JS Variables JS Let JS Const JS Operators JS Arithmetic JS Assignment JS Data Types JS Functions JS Objects JS Object Properties JS Object Methods JS Object Display JS Object Constructors JS Events JS Strings JS String Methods JS String Search JS String Templates JS Numbers JS BigInt JS Number Methods JS Number Properties JS Arrays JS Array Methods JS Array Search JS Array Sort JS Array Iteration JS Array Const JS Dates JS Date Formats JS Date Get Methods JS Date Set Methods JS Math JS Random JS Booleans JS Comparisons JS If Else JS Switch JS Loop For JS Loop For In JS Loop For Of JS Loop While JS Break JS Iterables JS Sets JS Set Methods JS Maps JS Map Methods JS Typeof JS Type Conversion JS Destructuring JS Bitwise JS RegExp JS Precedence JS Errors JS Scope JS Hoisting JS Strict Mode JS this Keyword JS Arrow Function JS Classes JS Modules JS JSON JS Debugging JS Style Guide JS Best Practices JS Mistakes JS Performance JS Reserved Words

JS 版本

JS 版本 JS 2009 (ES5) JS 2015 (ES6) JS 2016 JS 2017 JS 2018 JS 2019 JS 2020 JS 2021 JS 2022 JS 2023 JS 2024 JS IE / Edge JS 歷史

JS 物件

物件定義 物件原型 物件方法 物件屬性 物件 Get / Set 物件保護

JS 函式

函式定義 函式引數 函式呼叫 函式 Call 函式 Apply 函式 Bind 函式閉包

JS 類

類入門 類繼承 類靜態

JS 非同步

JS 回撥 JS 非同步 JS Promises JS Async/Await

JS HTML DOM

DOM 入門 DOM 方法 DOM Document DOM 元素 DOM HTML DOM 表單 DOM CSS DOM 動畫 DOM 事件 DOM 事件監聽器 DOM 導航 DOM 節點 DOM 集合 DOM 節點列表

JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS Web APIs

Web API 入門 Web 表單 API Web History API Web Storage API Web Worker API Web Fetch API Web Geolocation API

JS AJAX

AJAX 入門 AJAX XMLHttp AJAX Request AJAX Response AJAX XML 檔案 AJAX PHP AJAX ASP AJAX 資料庫 AJAX 應用 AJAX 示例

JS JSON

JSON 入門 JSON 語法 JSON vs XML JSON 資料型別 JSON 解析 JSON Stringify JSON 物件 JSON 陣列 JSON 伺服器 JSON PHP JSON HTML JSON JSONP

JS vs jQuery

jQuery 選擇器 jQuery HTML jQuery CSS jQuery DOM

JS Graphics

JS Graphics JS Canvas JS Plotly JS Chart.js JS Google Chart JS D3.js

JS 示例

JS 示例 JS HTML DOM JS HTML 輸入 JS HTML 物件 JS HTML 事件 JS 瀏覽器 JS 編輯器 JS 練習 JS 測驗 JS 網站 JS 面試準備 JS Bootcamp JS 證書

JS 參考

JavaScript 物件 HTML DOM 物件


JavaScript 字串搜尋

JavaScript String indexOf()

indexOf() 方法返回字串中 **第一個** 出現的子字串的 **索引**(位置),如果找不到該子字串,則返回 -1

示例

let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");
自己動手試一試 »

注意

JavaScript 的位置計數從零開始。

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


JavaScript String lastIndexOf()

lastIndexOf() 方法在字串中返回指定文字 **最後** 一次出現的 **索引**

示例

let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate");
自己動手試一試 »

如果找不到文字, indexOf()lastIndexOf() 都會返回 -1

示例

let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("John");
自己動手試一試 »

這兩個方法都接受第二個引數作為搜尋的起始位置

示例

let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate", 15);
自己動手試一試 »

lastIndexOf() 方法向後搜尋(從末尾到開頭),這意味著:如果第二個引數是 15,搜尋將從位置 15 開始,並搜尋到字串的開頭。

示例

let text = "Please locate where 'locate' occurs!";
text.lastIndexOf("locate", 15);
自己動手試一試 »

JavaScript String search()

search() 方法搜尋字串中的字串(或正則表示式),並返回匹配項的位置

示例

let text = "Please locate where 'locate' occurs!";
text.search("locate");
自己動手試一試 »
let text = "Please locate where 'locate' occurs!";
text.search(/locate/);
自己動手試一試 »

你注意到嗎?

這兩個方法, indexOf()search(),它們是 **相等** 的嗎?

它們接受相同的引數,並返回相同的值?

這兩個方法是 **不** 相等的。這些是區別

  • search() 方法不能接受第二個起始位置引數。
  • indexOf() 方法不能接受強大的搜尋值(正則表示式)。

你將在後面的章節中瞭解更多關於正則表示式的知識。



JavaScript String match()

match() 方法返回一個數組,其中包含將字串與字串(或正則表示式)匹配的結果。

示例

搜尋 "ain"

let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain");
自己動手試一試 »

搜尋 "ain"

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/);
自己動手試一試 »

執行全域性搜尋 "ain"

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
自己動手試一試 »

執行全域性、不區分大小寫的搜尋 "ain"

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
自己動手試一試 »

注意

如果正則表示式不包含g 修飾符(全域性搜尋),match() 將只返回字串中的第一個匹配項。

JS RegExp 章節中瞭解更多關於正則表示式的資訊。


JavaScript String matchAll()

matchAll() 方法返回一個迭代器,其中包含將字串與字串(或正則表示式)匹配的結果。

示例

const iterator = text.matchAll("Cats");
自己動手試一試 »

如果引數是正則表示式,則必須設定全域性標誌 (g),否則會丟擲 TypeError。

示例

const iterator = text.matchAll(/Cats/g);
自己動手試一試 »

如果要進行不區分大小寫的搜尋,則必須設定不區分大小寫的標誌 (i)

示例

const iterator = text.matchAll(/Cats/gi);
自己動手試一試 »

注意

matchAll() 是一個 ES2020 功能。

matchAll() 在 Internet Explorer 中不起作用。


JavaScript String includes()

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

否則返回 false

示例

檢查字串是否包含 "world"

let text = "Hello world, welcome to the universe.";
text.includes("world");
自己動手試一試 »

檢查字串是否包含 "world"。從位置 12 開始

let text = "Hello world, welcome to the universe.";
text.includes("world", 12);
自己動手試一試 »

注意

includes() 區分大小寫。

includes() 是一個 ES6 功能

includes() 在 Internet Explorer 中不受支援。


JavaScript String startsWith()

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

否則返回 false

示例

返回 true

let text = "Hello world, welcome to the universe.";
text.startsWith("Hello");
自己動手試一試 »

返回 false

let text = "Hello world, welcome to the universe.";
text.startsWith("world")
自己動手試一試 »

可以指定搜尋的起始位置

返回 false

let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5)
自己動手試一試 »

返回 true

let text = "Hello world, welcome to the universe.";
text.startsWith("world", 6)
自己動手試一試 »

注意

startsWith() 區分大小寫。

startsWith() 是一個 ES6 功能

startsWith() 不受 Internet Explorer 支援。


JavaScript String endsWith()

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

否則返回 false

示例

檢查字串是否以 "Doe" 結尾

let text = "John Doe";
text.endsWith("Doe");
自己動手試一試 »

檢查字串的前 11 個字元是否以 "world" 結尾

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

自己動手試一試 »

注意

endsWith() 區分大小寫。

endsWith() 是一個 ES6 功能

endsWith() 不受 Internet Explorer 支援。


完整字串參考

有關完整的字串參考,請訪問我們的

完整的 JavaScript 字串參考.

該參考包含了所有字串屬性和方法的說明及示例。

×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援