【学习笔记】JavaScript 寻找字串的方法:includes/indexOf/search/match

在处理数据时,查找字符串是一种常见的操作,JavaScript 提供不同的方法来搜索字符串。 其中,最常用的方法包括:search、indexOf、includes 和 match,能够辨别字串里是否有想要查找的文字:

  • String.prototype.search( )

  • String.prototype.indexOf( )

  • String.prototype.match()

  • String.prototype.includes()

接下来会比较这四种方法的不同之处,以及使用范例。


String.prototype.search( ):检测字符串是否包含,有的话回传 index,否则回传 -1


  • 搜索指定字符串,并返回匹配字符串第一个字的索引值

  • 若找不到,则返回 -1

  • 可支持正则表达式

search(regexp)

使用示例:

  • 字符匹配

let str ="Hello world!";let position = str.search("world");

console.log(position);// 6
  • 正则表达式匹配

let str ="Say hello to Hello World!";let rule1 = str.search(/[A-Z]/);// 符合大寫字母 A 到 Zlet rule2 = str.search(/Hello/);// 符合 Hellolet rule3 = str.search(/Hello/i);// i 代表不區分大小寫

console.log(rule1);// 0
console.log(rule2);// 13
console.log(rule3);// 4

String.prototype.indexOf():检测字符串是否包含,有的话回传 index,否则回传 -1


  • 搜索指定字符串,并返回匹配字符串第一个字的索引值

  • 若沒有找到,則返回 -1

  • 第二個參數 position 為選填屬性,代表從哪個 index 找起

  • 与 search() 方法类似,差别在于 indexOf() 不支持正则表达式

indexOf(searchString, position)

使用示例:

  • 找特定字符的 index

let str ="Say hello to hello world!";let position1 = str.indexOf("hello");let position2 = str.indexOf("hello",10);let position3 = str.indexOf("Hello",10);let position4 = str.indexOf("zzz");

console.log(position1);// 4
console.log(position2);// 13
console.log(position3);// -1
console.log(position4);// -1

String.prototype.match():寻找并取出内容,有的话以阵列回传,否则回传 null


  • 搜索指定字符串,并返回一个或多个与指定值匹配的数组

  • 若找不到则返回 null

  • 支持正则表达式

string.match(searchvalue)
string.match(regexp)

使用示例:

  • 字符串匹配

let str ="hello world";let result1 = str.match("hello");let result2 = str.match("Hello");

console.log(result1);// ['hello', index: 0, input: 'hello world', groups: undefined]
console.log(result2);// null
  • 正规表达式匹配:需注意要加上标志,才会返回匹配的所有结果g

找指定字符串 :

let str ="Say hello to hello World!";let result1 = str.match(/hello/);let result2 = str.match(/hello/g);

console.log(result1);// ['hello', index: 4, input: 'Say hello to hello World!', groups: undefined]
console.log(result2);// ['hello', 'hello']

找大写字母:

const paragraph ='Hello, I am fine. Thank you.';const regex =/[A-Z]/;const globalReg =/[A-Z]/g;

console.log(paragraph.match(regex));// ['H', index: 0, input: 'Hello, I am fine. Thank you.', groups: undefined]
console.log(paragraph.match(globalReg));// ["H", "I", "T"]

String.prototype.includes():判断字符串中是否包含指定字符串


  • 找到匹配字符串返回 true; 否则返回 flase

  • 不支持正则表达式

string.includes(searchvalue, start)

使用示例:

let str ="Hello world!";let isPresent1 = str.includes("world");let isPresent2 = str.includes("zzz");

console.log(isPresent1);// true
console.log(isPresent2);// false

小结


过去曾整理过阵列遍历的相关笔记:

项目中时常需要去搜索特定字符串,来达成特定目的。 翻找笔记时,才发现自己竟然还没有整理过这几个 JavaScript 提供的方法的差异。

也借由这个机会,又好好重新复习正则表达式的一些观念,虽然很多时候是需要用到的时候再查就好,但果然原生的底子还是基础中的基础。

此外,也体验了最近正夯的 ChatGPT,给一段关键词写出来的文章,差不多就完成了八七分架构,只需要再多补充一些观念,一篇笔记就热腾腾的诞生了ಠ_ಠ

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值