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

边栏推荐
- Introduction to two types of rxjs observable operators
- Basic syntax of unity script (3) - accessing game object components
- 基于ThinkPHP5封装-tronapi-波场接口-源码无加密-可二开--附接口文档-作者详细指导-2022年6月30日08:45:27
- Kaniko official documents - build images in kubernetes
- 防火墙基础之总部双机热备与分支基础配置
- postman 自动生成 curl 代码片段
- MySQL implements the division of two query results
- Golang Basics - string and int, Int64 inter conversion
- Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年6月29日21:59:34
- 【精选】资源变现资讯、新闻、自媒体、博客小程序(可引流,开通流量主,带pc后台管理)
猜你喜欢

Derivation of Park transformation formula for motor control

商品服务-平台属性
![[one day learning awk] array usage](/img/75/4333452142a3c7325e0712f3306985.png)
[one day learning awk] array usage
![[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

Basic syntax of unity script (1) - common operations of game objects

黑马笔记---List系列集合与泛型

Goods and services - platform properties
![[surprised] the download speed of Xunlei is not as fast as that of the virtual machine](/img/04/b0d23266b70c9ad6990a5203ef8ddf.png)
[surprised] the download speed of Xunlei is not as fast as that of the virtual machine

电机控制park变换公式推导

WTM重大更新,多租户和单点登录
随机推荐
Kubeedge's core philosophy
【 surprise】 la vitesse de téléchargement de Thunderbolt n'est pas aussi rapide que celle de la machine virtuelle
[one day learning awk] use of built-in variables
Sqlserver query code is 936 simplified Chinese GBK. Should I write 936 or GBK?
今日睡眠质量记录80分
Basic syntax of unity script (5) - vector
Motor control Clarke( α/β) Derivation of equal amplitude transformation
发生QQ大规模盗号事件,暴露出什么网络安全问题?
Unity脚本的基础语法(1)-游戏对象的常用操作
JS method of changing two-dimensional array to one-dimensional array
【驚了】迅雷下載速度竟然比不上虛擬機中的下載速度
kubeedge的核心理念
Tronapi- wavefield interface - source code without encryption - can be opened in two places - interface document attached - encapsulation based on thinkphp5 - detailed guidance of the author - 21:59:3
资源变现小程序开通流量主教程
波卡跨链通信源码探秘: 要素篇
Terms related to JMeter performance test and performance test passing standards
资源变现小程序开通微信官方小商店教程
Basic syntax of unity script (4) - access to other game objects
Database usage in QT
Development of unity script program