11、i与g模式修正符
let hd = 'wujUnjie';
console.log(hd.match(/u/gi))
console.log(hd.replace(/u/gi,'@'))
12、多行匹配修正符
let hd = `
#1 js,200元 #
#2 php,300元 #
#9 false.com # 吴俊杰
#3 node.js,180元 #`;
console.log(hd.match(/#\d+\s+.+#$/gm))
let obj = hd.match(/#\d+\s+.+#$/gm).map(v => {
v = v.replace(/#\d+\s+/,'').replace(/\s*#/,'');
[name,price] = v.split(',');
return {name, price};
})
console.log(obj)
13、汉字与字符属性
let junjie = '!wu你junj好ie,. 20污1军0姐;';
console.log(junjie.match(/\p{P}/gu))
console.log(junjie.match(/\p{sc=Han}/gu))
14、lastIndex属性的作用
let wu = 'wujunjie 2010';
let reg = /\w/g;
while(res = reg.exec(wu)) {
console.log(res)
}
15、有效率的y模式
let wu = 'unjieu iju;,u.u';
let reg = /u/g
let regy = /u/y
while(res = reg.exec(wu)) {
console.log(res)
}
while(res = regy.exec(wu)) {
console.log(res)
}
console.log(wu.match(/u/g))
let wuu = 'wunjieu iju;,u.u';
let regyy = /u/y;
regyy.lastIndex = 1;
while(res = regyy.exec(wuu)) {
console.log(res)
}
16、原子表基本使用
let jie = 'wujunjie';
console.log(jie.match(/[eu]/g));
let tel = '2022-03/23';
let reg = /^\d{4}[-\/]\d{2}[-\/]\d{2}/;
console.log(tel.match(reg));
let tel1 = '2022-03-23';
let reg1 = /^\d{4}([-\/])\d{2}\1\d{2}/;
console.log(tel1.match(reg1));
console.log(tel.match(reg1));
17、区间匹配
<input type="text" name="username">
let str = 'AAAwujunjie19960916BBByuanxiaorong00000';
console.log(str.match(/[a-z]+[0-9]+/g))
let user = document.querySelector(`[name='username']`);
user.addEventListener('keyup',function() {
console.log(this.value.match(/^[a-z]\w{3,6}$/i));
})
18、排除匹配
let str = 'wujunjie19960916';
console.log(str.match(/[^ue]/gi));
let newStr = `张三:010-999999,李四:020-8888888`;
console.log(newStr.match(/\p{sc=Han}+/gu));
console.log(newStr.match(/[^:\d\-,]+/g));
19、原子表字符不解析
let str = '(wujunjie).fun';
console.log(str.match(/[.+]/gi));
console.log(str.match(/./gi));
console.log(str.match(/()/gi));
console.log(str.match(/[()]/gi));
20、使用原子表匹配所有内容
let str = `
wujunjie
19960916
`;
console.log(str.match(/[\s]+/g));