原子表匹配:
let date = '2023-03-28';
let reg = /^\d{4}-\d{2}-\d{2}/;
let reg1 = /^\d{4}[-\/]\d{2}[-\/]\d{2}/;
let reg2 = /^\d{4}([-\/])\d{2}\1\d{2}/;
console.log(date.match(reg));//['2023-03-28', index: 0, input: '2023-03-28', groups: undefined]
console.log(date.match(reg1));//中间分隔无所谓,可以不相同['2023/03-28', index: 0, input: '2023/03-28', groups: undefined]
console.log(date.match(reg2));//中间分隔可以是- 也可以是/,但必须统一的,['2023-03-28', '-', index: 0, input: '2023-03-28', groups: undefined]
区间匹配:
let date = '2023';
console.log(date.match(/[0-9]+/g));//['2023'] [0-9] 只能是升序,不能写成[9-0]
let input = document.querySelector(`[name = "username"]`)
input.addEventListener('keyup',function(){
// 长度是4-7位,字母开头,可以包含字母数字 下划线
console.log(this.value.match(/^[a-z]\w{3,6}$/i));//['sdjf743', index: 0, input: 'sdjf743', groups: undefined]
})
排除匹配:
let str = 'jiajia佳佳:314515156';
console.log(str.match(/[^a]/g));//['j', 'i', 'j', 'i'] 除去a
console.log(str.match(/[^\w+:]+/g));//['佳佳']排除其他字符 拿到汉字
console.log(str.match(/\p{sc=Han}+/gu));//['佳佳'],复习利用字符方式拿到汉字