当前位置:网站首页>正则系列之量词(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^)\\\~~~
谢谢各位读者们啦(^_^)∠※
!!!
边栏推荐
- SAP GUI 里的收藏夹事务码管理工具
- How to write a performance test plan
- MySQL MHA high availability configuration and failover
- Relationship and difference between enterprise architecture and project management
- 104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
- 2020-ViT ICLR
- There is no signal in HDMI in computer games caused by memory, so it crashes
- QStringList 的常规使用
- 记录一次spark on yarn 任务报错 Operation category READ is not supported in state standby
- SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time
猜你喜欢
随机推荐
隐藏用户的创建和使用
There is no signal in HDMI in computer games caused by memory, so it crashes
Using securecrtportable to remotely connect virtual machines
Easyexcel complex data export
Mysql database detailed learning tutorial
Talk about what parameters ZABBIX monitors
Pytorch nn.functional.unfold()的简单理解与用法
leetcode - 287. 寻找重复数
2020-ViT ICLR
Appium automated testing foundation - Supplement: introduction to desired capabilities parameters
Two schemes of transforming the heat map of human posture estimation into coordinate points
MySQL的视图练习题
mixconv代码
Recent public ancestor offline practice (tarjan)
Awoo's favorite problem (priority queue)
twenty million two hundred and twenty thousand seven hundred and one
人体姿态估计的热图变成坐标点的两种方案
LC669. 修剪二叉搜索树
使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏
陈天奇的机器学习编译课(免费)