dotAll(点匹配)数组
named captured groups(命名分组捕获)学习
ES5spa
loodbehind assert(后行断言)code
正则中的点就是dotAll
,都是匹配任意字符,可是不少字符是没法匹配的。例如:blog
UTF-16
的字符\\n
\\r
换行 回车console.log(/foo.bar/.test('foo\nbar')) //false console.log(/foo.bar/.test('fooabar')) // true
加上s
能够匹配换行符rem
console.log(/foo.bar/s.test('foo\nbar')) // true
加上s
能够匹配换行符,加上u
就能够匹配4位的UTF-16
字符,点的功能就全能了字符串
console.log(/foo.bar/us.test('foo\nbar'))
使用flags
属性判断,若是没有开启就是false
input
const r = /foo.bar/ console.log(r.dotAll) // false console.log(r.flags) // 空 const re = /foo.bar/s console.log(re.dotAll) // true console.log(re.flags) // s
以前分组捕获有,可是命名的分组捕获刚有it
如何取到字符串中匹配的年月日?console
// 先看一下match匹配出来的值有哪些? console.log("2019-06-07".match(/(\d{4})-(\d{2})-(\d{2})/)) // ["2019-06-07", "2019-06-07", "2019", "06", "07", index: 0, input: "2019-06-07", groups: undefined] // 完整匹配 // 第一个括号分组 // 第二个括号分组 // 第三个括号分组 // index 从第几个字符开始匹配到的 // input 完整的输入字符串 // groups 目前位空,一会就知道用法了 const t = "2019-06-07".match(/(\d{4})-(\d{2})-(\d{2})/) console.log(t[1]) //2019 console.log(t[2]) //06 console.log(t[3]) //07
上面的方法,若是数据复杂的时候很差写,何况数组还要数第几个,那么加一个名字会比较好
// 在括号里面写 ?<命名key> const T = "2019-06-07".match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/) console.log(T) // ["2019-06-07", "2019", "06", "07", index: 0, input: "2019-06-07", // groups: {day: "07", month: "06", year: "2019"}] // 这个时候groups里面有值了,并且用命名的key能够取到 console.log(T.groups.year) //2019 console.log(T.groups.month) //06 console.log(T.groups.day) //07
先行断言都有,ES9
刚有的后行断言
js
一直是先行断言的
let test = 'hello world' console.log(test.match(/hello(?=\sworld)/)) // 后面的括号不是分组匹配,是先行断言 // 先遇到一个条件
可是我要知道world
以前的那个是hello
,就是后行断言
ES9
这个能力补齐了
console.log(test.match(/(?<=hello\s)world/)) // (?<=hello\s) 是判断world前面是hello加空格 console.log(test.match(/(?<!hell2\s)world/)) // ! 表示不等于 // \1表示捕获匹配