当前位置:网站首页>正则系列之量词(Quantifiers)
正则系列之量词(Quantifiers)
2022-07-01 21:50:00 【老__L】
量词表示要匹配的字符或表达式的数量。
1、x*
将前面的项“x”匹配 0 次或更多次。
let reg = /bo*/g;
let str = "A ghost booooed";
str.match(reg); // ['boooo']
str = "A bird warbled";
str.match(reg); // ['b', 'b']
str = "A goat grunted";
str.match(reg); // null
2、x+
将前一项“x”匹配 1 次或更多次,等价于{1,}。
let reg = /bo+/g;
let str = "A ghost booooed";
str.match(reg); // ['boooo']
str = "A bird warbled";
str.match(reg); // null
str = "A goat grunted";
str.match(reg); // null
reg = /a+/g;
str = "caaaaaaandy";
str.match(reg); // ['aaaaaaa']
3、x?
将前面的项“x”匹配 0 或 1 次。
如果在任何量词*、+、?或{}之后使用?,则使量词是非贪婪的 (匹配最小次数),而不是默认的贪婪的 (匹配最大次数)。
let reg = /bo?/g;
let str = "A ghost booooed";
str.match(reg); // ['bo']
str = "A bird warbled";
str.match(reg); // ['b', 'b']
str = "A goat grunted";
str.match(reg); // null
reg = /bo??/g;
str = "A ghost booooed";
str.match(reg); // ['b']
4、x{n}
其中“n”是一个正整数,与前一项“x”的 n 次匹配。
let reg = /a{2}/g;
let str = "candy";
str.match(reg); // null
str = "caandy";
str.match(reg); // ['aa']
str = "caaandy";
str.match(reg); // ['aa']
5、x{n,}
其中,“n”是一个正整数,与前一项“x”至少匹配“n”次。
let reg = /a{2,}/g;
let str = "caaandy";
str.match(reg); // ['aaa']
6、x{n,m}
其中n为0或正整数,m为正整数,且m > n,前一项x至少匹配n次且最多匹配m次。
let reg = /a{2,4}/g;
let str = "caaaaaaaaaaandy";
str.match(reg); // ['aaaa', 'aaaa', 'aaa']
7、x*?x+?x??x{n}?x{n,}?x{n,m}?
默认情况下,像*和+这样的量词是“贪婪的”,这意味着它们试图匹配尽可能多的字符串。量词后面的字符?使量词“非贪婪”:这意味着一旦找到匹配,它就会停止。
let reg = /a{2,4}?/g;
let str = "caaaaaaaaaaandy";
str.match(reg); // ['aa', 'aa', 'aa', 'aa', 'aa']
后记
如果你感觉文章不咋地
//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O???
如果你觉得该文章有一点点用处,可以给作者点个赞;\\*^o^*//
如果你想要和作者一起进步,可以微信扫描二维码,关注前端老L;~~~///(^v^)\\\~~~
谢谢各位读者们啦(^_^)∠※!!!

边栏推荐
- 多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
- spark analyze命令使用及其作用 map join broadcast join 广播join
- 20220701
- MySQL5.7 设置密码策略(等保三级密码改造)
- Chen Tianqi's machine learning compilation course (free)
- 104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
- Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
- [literacy] deep / shallow, local / global features in machine learning image processing
- Delete AWS bound credit card account
- # CutefishOS系统~
猜你喜欢

今日睡眠质量记录71分

Favorite transaction code management tool in SAP GUI
![[untitled]](/img/60/9a56e8b00c386779be13308515b24f.png)
[untitled]

How to write a performance test plan

固定资产管理子系统报表分为什么大类,包括哪些科目

Understanding of transactions in MySQL

Recent public ancestor (LCA) online practices

The fixed assets management subsystem reports are divided into what categories and which accounts are included

Slope compensation

性能测试计划怎么编写
随机推荐
元宇宙可能成为互联网发展的新方向
【c语言】malloc函数详解[通俗易懂]
【JetCache】JetCache的使用方法与步骤
Origin2018 installation tutorial "recommended collection"
Operation category read is not supported in state standby
牛客月赛-分组求对数和
Appium自动化测试基础 — 补充:Desired Capabilities参数介绍
JVM有哪些类加载机制?
MySQL view exercise
leetcode - 287. 寻找重复数
快乐数[环类问题之快慢指针]
[jetcache] how to use jetcache
C#/VB. Net to add text / image watermarks to PDF documents
Rust language - Introduction to Xiaobai 05
Using securecrtportable to remotely connect virtual machines
Delete AWS bound credit card account
倒置残差的理解
MySQL中对于事务的理解
MySQL MHA high availability configuration and failover
【图像分割】2021-SegFormer NeurIPS