摘要:正则表达式是程序员的必备技能,想不想多学几招呢?
本文用JavaScript的exec方法来测试正则表达式。
例如,正则表达式/F.*g/会匹配“以F开头,以g结尾的字符串”,因此可以匹配”Hello, Fundebug!”中的Fundebug,exec方法会返回一个数组,其第一个元素为所匹配的子字符串。
| /F.*g/.exec("Hello, Fundebug!")0// 'Fundebug' |
|:----|
默认情况下,正则表达式的量词*、+、?、{},都是进行贪婪匹配,即匹配尽可能多的字符。
例如,正则表达式/.+\s/匹配的是“以空格符结尾的字符串”,我们用它来匹配苹果公司创始人乔布斯在斯坦福大学演讲的名言“You time is limited, so don’t waste it living someone else’s life.”:
| /.+\s/.exec("You time is limited, so don’t waste it living someone else’s life.")0// 'You time is limited, so don’t waste it living someone else’s ' |
|:----|
.可以匹配任意字符,而+表示匹配1次或者多次,且是贪婪的,因此/.+\s/匹配到了最后一个空格符才结束。
当我们在量词*、+、?、{}后面紧跟着一个?,就可以实现非贪婪匹配,即匹配尽量少的字符。
例如,正则表达式/.+?\s/匹配到第一个空格符就会结束:
| /.+?\s/.exec("You time is limited, so don’t waste it living someone else’s life.")0// 'You ' |
|:----|
使用正则表达式x(?=y),可以匹配’x’仅仅当’x’后面跟着’y’。这话有点绕,简单地说,就是匹配后面是y的x,这里的x和y都代表正则表达式。
例如,对于博客RabbitMQ入门教程的地址”[https://blog.fundebug.com/2018/04/20/rabbitmq_tutorial/",如果需要匹配出域名fundebug的话,可以使用**/[a-z]+(?=.com)/**,匹配“在.com前面的英文单词”](https://blog.fundebug.com/2018/04/20/rabbitmq_tutorial/%22%EF%BC%8C%E5%A6%82%E6%9E%9C%E9%9C%80%E8%A6%81%E5%8C%B9%E9%85%8D%E5%87%BA%E5%9F%9F%E5%90%8Dfundebug%E7%9A%84%E8%AF%9D%EF%BC%8C%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8/[a-z]+(?=.com%29/%EF%BC%8C%E5%8C%B9%E9%85%8D%E2%80%9C%E5%9C%A8.com%E5%89%8D%E9%9D%A2%E7%9A%84%E8%8B%B1%E6%96%87%E5%8D%95%E8%AF%8D%E2%80%9D)
| /a-z+(?=.com)/.exec("https://blog.fundebug.com/2018/04/20/rabbitmq_tutorial/")0// 'fundebug' |
|:----|
广告:欢迎免费试用Fundebug,为您监控线上代码的BUG,提高用户体验~
与正向肯定查找所对应的是正向否定查找,使用正则表达式x(?!y),可以”匹配’x’仅仅当’x’后面不跟着’y’”。
例如,小学生都知道的圆周率是3.1415926,不会的同学可以这样记“山顶上有一座寺庙,寺庙里面有一壶酒,还有一块肉”。如何匹配小数点后面的数字呢?可以使用/\d+(?!.)/,匹配”后面没有小数点的数字”:
| /\d+(?!.)/.exec("3.1415926")0// '1415926' |
|:----|
而使用之前提到的正向肯定查找,就可以匹配小数点前面的数字:
| /\d+(?=.)/.exec("3.1415926")0// '3' |
|:----|
下面是鲍勃·迪伦的《Forever Young》歌词:
| May God bless and keep you always,may your wishes all come true,may you always do for othersand let others do for you.may you build a ladder to the starsand climb on every rung,may you stay forever young,forever young, forever young,May you stay forever young. |
|:----|
如何匹配以forever开头的那句歌词forever young, forever young呢?
这样写/^forever.+/是错误的:
| /^forever.+/.exec("May God bless and keep you always,\nmay your wishes all come true,\nmay you always do for others\nand let others do for you.\nmay you build a ladder to the stars\nand climb on every rung,\nmay you stay forever young,\nforever young, forever young,\nMay you stay forever young.")// null |
|:----|
为什么错了?因为^匹配的整个字符串的开始,而是不是每一行的开始。
正则表达式指定m选项,即可支持多行匹配,这时^和$匹配的是每一行的开始和结束,因此正确的正则表达式是/^forever.+/m:
| /^forever.+/m.exec("May God bless and keep you always,\nmay your wishes all come true,\nmay you always do for others\nand let others do for you.\nmay you build a ladder to the stars\nand climb on every rung,\nmay you stay forever young,\nforever young, forever young,\nMay you stay forever young.")0// 'forever young, forever young,' |
|:----|
在正则表达式中使用小括号(),可以提取出字符串中的特定子串。
例如,Fundebug是在2016年双11正式上线的,时间是”2016-11-11”,如何提取其中的年、月、日呢?如下:
| /(\d{4})-(\d{2})-(\d{2})/.exec("2016-11-11")// '2016-11-11', '2016', '11', '11', index: 0, input: '2016-11-11' |
|:----|
可知,3个小括号中的正则表达式分别匹配的是年月日,其结果依次为exec返回数组中的1到3号元素。
版权声明:
转载时请注明作者Fundebug以及本文地址:
https://blog.fundebug.com/2018/05/02/advanced_regular_expression/
您的用户遇到BUG了吗?
.copyright * { box-sizing: border-box; }