导航
×
   ❮   
HTML CSS JavaScript PHP Go ECMS

JavaScript 教程

JS 简介 JS 如何使用 JS 输出 JS 语法 JS 语句 JS 注释 JS 变量 JS 运算符 JS 条件语句 JS 循环 JS 字符串 JS 数字 JS 函数 JS 对象 JS 日期 JS 数组 JS 类型化数组 JS 集合 JS Map 映射 JS Math JS 正则表达式 JS 数据类型 JS 错误 JS 事件 JS 编程 JS 关键字参考 JS 保留关键字参考 JS 运算符参考 JS 运算符优先级 JS UTF-8 字符 JS UTF-8 符号 JS UTF-8 表情符号 JS 版本

JavaScript 字符串搜索



JavaScript 字符串 indexOf()

indexOf() 方法返回字符串中某个字符串首次出现的位置(索引),如果找不到该字符串,则返回 -1。

示例

let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");
亲自试一试 »

注意

JavaScript 从零开始计数字符串中的位置。

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


JavaScript 字符串 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 字符串搜索()

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 字符串匹配()

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 字符串 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 字符串包含方法

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 特性


JavaScript 字符串 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 特性


JavaScript 字符串以指定值结尾

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 特性


完整的 JavaScript 参考

如需查看包含完整描述和大量示例的 JavaScript 属性和方法的完整参考,请访问:

FreeW3C 的完整 JavaScript 参考

该参考包含 1999 年至 2025 年的所有 JavaScript 更新。


freew3c.com 中文网是独立运营的中文开发者学习平台,与 freew3c.com 无关联。提供的内容仅用于学习和测试,不保证内容的正确性。


Copyright @2020-2026 京ICP备888888号-8