当前位置:网站首页>正则系列之量词(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^)\\\~~~
谢谢各位读者们啦(^_^)∠※!!!

边栏推荐
- CIO's discussion and Analysis on the definition of high-performance it team
- 多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
- Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
- 删除AWS绑定的信用卡账户
- RestTemplate 远程调用工具类
- 元宇宙可能成为互联网发展的新方向
- Ffmpeg learning notes
- Selection of all-optical technology in the park - Part 2
- internal field separator
- 基准环路增益与相位裕度的测量
猜你喜欢

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

Pytorch sharpening chapter | argmax and argmin functions

Slope compensation

Mysql5.7 set password policy (etc. three-level password transformation)

【无标题】

Dark horse programmer - software testing - stage 06 2-linux and database-01-08 Chapter 1 - description of the content of the Linux operating system stage, description of the basic format and common fo

2020-ViT ICLR

Single step debugging analysis of rxjs observable of operator

Clean up system cache and free memory under Linux

Appium自动化测试基础 — 补充:Desired Capabilities参数介绍
随机推荐
SAP GUI 里的收藏夹事务码管理工具
Understanding of transactions in MySQL
13th Blue Bridge Cup group B national tournament
Pytorch nn.functional.unfold()的简单理解与用法
Rust language - Introduction to Xiaobai 05
Niuke monthly race - logarithmic sum in groups
好友新书发布,祝贺(送福利)
MySQL的存储过程
Recent public ancestor offline practice (tarjan)
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条
【图像分割】2021-SegFormer NeurIPS
spark analyze命令使用及其作用 map join broadcast join 广播join
Redis configuration and optimization
MySQL中对于事务的理解
The fixed assets management subsystem reports are divided into what categories and which accounts are included
内部字段分隔符
Ffmpeg learning notes
GenICam GenTL 标准 ver1.5(4)第五章 采集引擎
MySQL的视图练习题
Intelligent computing architecture design of Internet