JavaScript


文章目录

  • JavaScript
  • 20 JavaScript 字符串搜索
  • 20.1 String.indexOf()
  • 20.2 String.lastIndexOf()
  • 20.3 String.search()
  • 20.4 String.match()
  • 20.5 String.includes()
  • 20.6 String.startsWith()
  • 20.7 String.endsWith()


20 JavaScript 字符串搜索

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

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

20.1 String.indexOf()

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

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

javascript字符串查询 javascript 字符串查找_开发语言

这里需要新开一个页面,let不能 重新定义已经存在的var 变量。

JavaScript 从零开始计算位置。

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

20.2 String.lastIndexOf()

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

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

javascript字符串查询 javascript 字符串查找_开发语言_02

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

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

javascript字符串查询 javascript 字符串查找_字符串_03

这两种方法都接受第二个参数作为搜索的开始位置:

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

javascript字符串查询 javascript 字符串查找_javascript字符串查询_04

lastIndexOf() 方法向后搜索(从末尾到开头),意思是:如果第二个参数是 15,则从位置 15 开始搜索,一直搜索到字符串的开头。

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

javascript字符串查询 javascript 字符串查找_javascript_05

20.3 String.search()

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

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

javascript字符串查询 javascript 字符串查找_开发语言_06

indexOf()search() 这两个方法,相等吗?

它们接受相同的参数,并返回相同的值?

这两种方法并不相等。差别如下:

  • search() 方法不能接受第二个起始位置参数。
  • indexOf() 方法不能采用强大的搜索值(正则表达式)。

20.4 String.match()

match() 方法根据正则表达式在字符串中搜索匹配项,并将匹配项作为 Array 对象返回。

【在字符串中搜索 “ain”:】

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

javascript字符串查询 javascript 字符串查找_javascript字符串查询_07

如果正则表达式不包含 g 修饰符(执行全局搜索),match() 方法将只返回字符串中的第一个匹配项。

javascript字符串查询 javascript 字符串查找_开发语言_08

【语法】

string.match(regexp)

javascript字符串查询 javascript 字符串查找_前端_09

【对 “ain” 执行不区分大小写的全局搜索:】

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

javascript字符串查询 javascript 字符串查找_开发语言_10

20.5 String.includes()

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

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

javascript字符串查询 javascript 字符串查找_javascript_11

【浏览器支持】

Internet Explorer 不支持 String.includes()。

javascript字符串查询 javascript 字符串查找_javascript字符串查询_12

【语法】

string.includes(searchvalue, start)

javascript字符串查询 javascript 字符串查找_开发语言_13

检查字符串是否包含 “world”,从位置 12 开始搜索:

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

javascript字符串查询 javascript 字符串查找_字符串_14

20.6 String.startsWith()

如果字符串以指定值开头,则 startsWith() 方法返回 true,否则返回 false

let text = "Hello world, welcome to the universe.";

text.startsWith("Hello")   // 返回 true

javascript字符串查询 javascript 字符串查找_字符串_15

【语法】

string.startsWith(searchvalue, start)

【参数值】

javascript字符串查询 javascript 字符串查找_开发语言_16

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() 方法。

第一个完全支持的浏览器版本是:

javascript字符串查询 javascript 字符串查找_字符串_17

20.7 String.endsWith()

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

检查字符串是否以 “Gates” 结尾:

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

javascript字符串查询 javascript 字符串查找_javascript字符串查询_18

【语法】

string.endsWith(searchvalue, length)

【参数值】

javascript字符串查询 javascript 字符串查找_字符串_19

检索以 “world” 结尾的字符串的前 11 个字符:

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

注释:endsWith() 方法区分大小写。

Internet Explorer 不支持 endsWith() 方法。

第一个完全支持该方法的浏览器版本是:

javascript字符串查询 javascript 字符串查找_字符串_20