JS正则表达式的i m g
编程技术  /  houtizong 发布于 3年前   172
g:表示全局(global)模式,即模式将被应用于所有字符串,而非在发现第一个匹配项时立即停止。
i:表示不区分大小写(case-insensitive)模式,即在确定匹配项时忽略模式与字符串的大小写。
m:表示多行(multiline)模式,即在到达一行文本末尾时还会继续查找下一行中是否存在与模式匹配的项。
实例:
var pattern = /^a.+b$/; undefined "adfasdfasdfasdfasdf".match(pattern) null "adfasdfasdfasdfasdb".match(pattern) ["adfasdfasdfasdfasdb"] "adfasdfasdfasdfasdb\r\naasdfasdfasdf".match(pattern) null "adfasdfasdfasdfasdb\r\naasdfasdfasdfb".match(pattern) null var pattern = /^a.+b$/m; undefined "adfasdfasdfasdfasdb\r\naasdfasdfasdfb".match(pattern) ["adfasdfasdfasdfasdb"] var pattern = /^a.+b$/g; undefined "adfasdfasdfasdfasdb\r\naasdfasdfasdfb".match(pattern) null var pattern = /^a.+b$/mg; undefined "adfasdfasdfasdfasdb\r\naasdfasdfasdfb".match(pattern) ["adfasdfasdfasdfasdb", "aasdfasdfasdfb"]
从这几个实例不难看出:m是多行匹配,在配合^ $的正则表达式中,强制匹配每一行的开头结束
但对g作用又有点迷糊了,下面是g的实例:
var pattern = /^a.+b$/g; undefined "adfasdfasdfasdfasdb\r\naasdfasdfasdfb".match(pattern) null var pattern = /a.+b$/g; undefined "adfasdfasdfasdfasdb\r\naasdfasdfasdfb".match(pattern) ["aasdfasdfasdfb"] var pattern = /^a.+b/g; undefined "adfasdfasdfasdfasdb\r\naasdfasdfasdfb".match(pattern) ["adfasdfasdfasdfasdb"] var pattern = /a.+b/g; undefined "adfasdfasdfasdfasdb\r\naasdfasdfasdfb".match(pattern) ["adfasdfasdfasdfasdb", "aasdfasdfasdfb"]
附:
^:匹配一个输入或一行的开头,/^a/匹配"an A",而不匹配"An a"
$:匹配一个输入或一行的结尾,/a$/匹配"An a",而不匹配"an A"
对如下执行结果还是没太弄明白:
var pattern = /^a.+b$/g; undefined "adfasdfasdfasdfasdb\r\naasdfasdfasdfb".match(pattern) null
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接