菜单
×
   ❮     
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 提供支持