当前位置:网站首页>正则系列之断言Assertions
正则系列之断言Assertions
2022-06-30 12:40:00 【老__L】
断言的组成之一是边界。对于文本、词或模式,边界可以用来表明它们的起始或终止部分(如向前断言,向后断言以及条件表达式)。
1、边界类断言
1.1、^
匹配输入的开头。如果多行模式设为 true,^ 在换行符后也能立即匹配。
let reg = /^1[3-9]\d{9}/gm;
let str = "25000000000";
reg.test(str); // false
str = "15000000000\n17000000000";
let found = str.match(reg); // ['15000000000', '17000000000']
1.2、$
匹配输入的结束。如果多行模式设为 true,$ 在换行符前也能立即匹配。
let reg = /^1[3-9]\d{9}7$/gm;
let str = "25000000000";
reg.test(str); // false
str = "15000000000\n170000000007";
let found = str.match(reg); // ['170000000007']
1.3、\b
匹配一个单词的边界(边界包括换行符和空格),这是一个字的字符前 / 后没有另一个字的字符位置。
let reg = /\bm/gm;
let str = "month\nmoom";
str.match(reg); // ['m', 'm']
reg = /th\b/gm;
str = "month\nthird";
str.match(reg); // ['th']
str = "month\nsomething";
str.match(reg); // ['th']
str = "month ufo";
str.match(reg); // ['th']
1.4、\B
匹配非单词边界。
let reg = /on\B/gm;
let str = "at noon";
str.match(reg); // null
str = "noon noon\nnoon";
str.match(reg); // null
str = "ongoing";
str.match(reg); // ['on']
2、其他断言
2.1、x(?=y)
向前断言: x 被 y 紧随时匹配 x,结果不包括 y。
let reg = /\d(?=y)/g;
let str = "1y2y3";
str.match(reg); // ['1', '2']
2.2、x(?!y)
向前否定断言: x 没有被 y 紧随时匹配 x。
let reg = /\d(?!y)/g;
let str = "1y2y3";
str.match(reg); // ['3']
2.3、(?<=y)x
向后断言: x 跟随 y 的情况下匹配 x,结果不包括 y。
let reg = /(?<=y)\d/g;
let str = "y1y23";
str.match(reg); // ['1', '2']
2.4、(?<!y)x
向后否定断言: x 不跟随 y 时匹配 x。
let reg = /(?<!y)\d/g;
let str = "y1y23";
str.match(reg); // ['3']
后记
如果你感觉文章不咋地
//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O???
如果你觉得该文章有一点点用处,可以给作者点个赞;\\*^o^*//
如果你想要和作者一起进步,可以微信扫描二维码,关注前端老L;~~~///(^v^)\\\~~~
谢谢各位读者们啦(^_^)∠※!!!

边栏推荐
- elementui中清除tinymce富文本缓存
- Docker installation of mysql8 and sqlyong connection error 2058 solution [jottings]
- The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!
- ABAP toolbox v1.0 (with implementation ideas)
- Development of unity script program
- Qt中的事件处理
- 一条查询SQL是如何执行的
- 防火墙基础之总部双机热备与分支基础配置
- How to handle ZABBIX server startup failure
- RK356x U-Boot研究所(命令篇)3.3 env相关命令的用法
猜你喜欢

QT read / write excel--qxlsx worksheet display / hide status setting 4

微信小程序报错:TypeError: Cannot read property ‘setData‘ of undefined

Data Lake (11): Iceberg table data organization and query

postman 自动生成 curl 代码片段

深度长文探讨Join运算的简化和提速
![[recruitment (Guangzhou)] Chenggong Yi (Guangzhou) Net core middle and Senior Development Engineer](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[recruitment (Guangzhou)] Chenggong Yi (Guangzhou) Net core middle and Senior Development Engineer

数据湖(十一):Iceberg表数据组织与查询

Kaniko official documents - build images in kubernetes

rxjs Observable 两大类操作符简介

Derivation of Park transformation formula for motor control
随机推荐
Paper interpretation (AGC) attributed graph clustering via adaptive graph revolution
Database usage in QT
Discussion on JMeter operation principle
Dark horse notes - common date API
Visual studio configures QT and implements project packaging through NSIS
FFMpeg AVBufferPool 的理解与掌握
Wechat applet reports an error: typeerror: cannot read property 'SetData' of undefined
波卡跨链通信源码探秘: 要素篇
Determining the subject area of data warehouse construction
uniapp支付之APP微信支付unicloud版(附源码)
Terms related to JMeter performance test and performance test passing standards
golang文件的写入、追加、读取、复制操作:bufio包的使用示例
Development of unity script program
[recruitment (Guangzhou)] Chenggong Yi (Guangzhou) Net core middle and Senior Development Engineer
postman 自动生成 curl 代码片段
The spiral matrix of the force buckle rotates together (you can understand it)
MySQL queries the data within the radius according to the longitude and latitude, and draws a circle to query the database
[one day learning awk] use of built-in variables
In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity
elementui中清除tinymce富文本缓存