正则表达式
1.具名组
let reg = /(?<year>\d{4})\/(?<month>\d{1,2})\/(?<date>\d{1,2})/ const res = reg.exec('2020/3/23') console.log(res)
2.引用
let str = '10/10/10' let r = /(?<year>\d{2})\/(\k<year>)/ console.log(r.exec(str))
3.正则方法
match() 、replace()、search()、split()
matchAll() 取出所有匹配,iterator遍历
const reg1 = /ickt\d/g const str1 = 'ickt1ickt2ickt3ickt4' console.log(reg1.exec(str1)) console.log(reg1.exec(str1)) console.log(reg1.exec(str1)) console.log(reg1.exec(str1)) console.log(str1.match(reg1)) console.log([...str1.matchAll(reg1)])
4.demo
1.匹配 "id":1223 的形式
const str ='[{"createUser":1435892060572876800,"id":1584453765346361344,"value":""}]' const arr = str .match(/"id":\d+/g)
2.手机号格式验证,现在除 12 开头的,其他的应该都有了
/^1[3456789]\d{9}$/