前言 之前对正则的理解一直是半懂不懂,现在和houdunren学一波正则。本篇是js正则相关的案例与细节学习笔记
十分建议配套quokka使用,是vscode一个显示代码实时结果的一个插件,非常方便.
学习笔记GitHub仓库:传送门
取出数字 1 2 3 4 5 6 7 8 9 let test = "jojo123jojo321" ;let num = [...test].filter(n => !Number .isNaN(parseInt (n)));console .log(num.join('' ))console .log(test.match(/\d/g ).join('' ))
字面量创建正则/存放变量 1 2 3 4 5 6 7 let test = "jojo123jojo321" ;console .log(/o/ .test(test))let useO = 'o' ;console .log(eval (`/${useO} /` ).test(test))
对象创建正则 1 2 3 4 5 6 7 8 9 10 11 12 let test = "jojolin" ;let a = "lin" ;let reg = new RegExp (a, "g" ); console .log(reg.test(test));test = test.replace(/\w/g ,(str )=> { return 'dio' }) console .log(test)
选择符 1 2 3 4 5 6 7 8 9 let test = "jojolin" ;console .log(/j|k/ .test(test))let tel = '010-9999999' let tel2 = '010' console .log(/(010|020)\-\d{7,8}/ .test(tel))console .log(/(010|020)\-\d{7,8}/ .test(tel2))
原子表和原子组 1 2 3 4 5 6 7 8 9 let reg = /[123456]/ let hd = '123123' console .log(hd.match(reg))let reg2 = /(12|34)/ ;console .log(hd.match(reg2))
正则表达式中的转义 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 let price = 23.34 console .log(/\d+\.\d+/ .test(price))let str = "\\d+\\.\\d+" ;console .log(str)let reg = new RegExp (str)console .log(reg.test(price)) let url = 'https://www.baidu.com' ;console .log(/^https?:\/\/\w+\.\w+\.\w+/ .test(url))
字符边界约束 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 let test = "123" ;console .log(/^\d$/ .test(test))console .log(/^\d+$/ .test(test))const reg = /^[a-z]{3,6}$/ console .log('' .match(reg))console .log('a' .match(reg))console .log('ab' .match(reg))console .log('abc' .match(reg))console .log('abcd' .match(reg))console .log('abcde' .match(reg))console .log('abcdef' .match(reg))console .log('abcdefg' .match(reg))
数值与空白元字符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 let hd = ` 张三:010-9999999,李四:020-99999999 ` ;console .log(hd.match(/[^\d,:-\s]+/g ));
w与W元字符 1 2 3 4 5 6 7 let email = '9813123123@qq.com' console .log(email.match(/^\w+@\w+\.\w+$/ ));
点元字符的使用 1 2 3 4 5 6 7 8 9 let hd = ` jojo jojolin ` console .log(hd.match(/.+/ )[0 ])console .log(hd.match(/.+/ s))
匹配所有字符 1 2 3 4 5 6 7 8 9 let hd = ` <span> jojolin @abc @@@cajsocj </span> ` console .log(hd.match(/<span>[\s\S]+<\/span>/ ))
模式修正符gi 1 2 3 4 5 6 7 8 9 let text = 'jojoJOJOJO' console .log(text.match(/o/gi ))console .log(text.replace(/o/gi ,'?' ))
模式修正符m 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 let hd = ` #1 js,200元 # #2 php,300元 # #55 houdunren.com # 后盾人 #56 nodejs,180元 # ` ;console .log(hd.match(/\s*#\d+\s+.+\s+#/g ));console .log(hd.match(/^\s*#\d+\s+.+\s+#$/gm ));let lessons = hd.match(/^\s*#\d+\s+.+\s+#$/gm ).map((v ) => { v = v.replace(/^\s*#\d+\s+/ , "" ).replace(/\s#$/ , "" ) console .log(v) let [name,price] = v.split(',' ); return { name,price } }); console .log(JSON .stringify(lessons,null ,2 ))
模式修正符u 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 let text = 'jojostoneocean2022.,}[com笑死我了哈哈哈' console .log(text.match(/\p{L}/gu ))console .log(text.match(/\p{P}/gu ))console .log(text.match(/\p{sc=Han}/gu ))
lastIndex属性的使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 let text = 'jojo' console .log(text.match(/\w/ ))console .log(text.match(/\w/g ))const reg = /\w/g console .log(reg.lastIndex)console .log(reg.exec(text))console .log(reg.lastIndex)console .log(reg.exec(text))console .log(reg.lastIndex)console .log(reg.exec(text))console .log(reg.lastIndex)console .log(reg.exec(text))console .log(reg.lastIndex)const reg2 = /\w/g while ((res = reg2.exec(text))){ console .log(res) } console .log(res) const reg3 = /\w/ console .log(reg3.lastIndex)console .log(reg3.exec(text))console .log(reg3.lastIndex)console .log(reg3.exec(text))console .log(reg3.lastIndex)console .log(reg3.exec(text))console .log(reg3.lastIndex)console .log(reg3.exec(text))console .log(reg3.lastIndex)console .log(reg.lastIndex)console .log(reg.exec(text))console .log(reg.lastIndex)console .log(reg.exec(text))console .log(reg.lastIndex)console .log(reg.exec(text))console .log(reg.lastIndex)console .log(reg.exec(text))console .log(reg.lastIndex)
有效率的y模式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 let text = 'asdasdasdas的,213123123,33333,asdasd' const reg = /\d/g console .log(reg.exec(text))console .log(reg.exec(text))console .log(reg.exec(text))console .log(reg.exec(text))console .log(reg.exec(text))console .log(reg.exec(text))console .log(reg.exec(text))const reg2 = /(\d+,?)/y reg2.lastIndex = 13 while ((res=reg2.exec(text))){ console .log(res) }
原子表原子组 新使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 let date = '2022-01-22' let date2 = '2022/01/22' ;let date3 = '2022-01/22' ;const reg = /^\d{4}[-\/]\d{2}[-\/]\d{2}/ console .log(reg.exec(date))console .log(reg.exec(date2))console .log(reg.exec(date3))const reg2 = /^\d{4}([-\/])\d{2}\1\d{2}/ console .log(reg2.exec(date))console .log(reg2.exec(date2))console .log(reg2.exec(date3))
区间匹配 1 2 3 4 5 6 7 let text = '213123jojoj' ;console .log(text.match(/[0-9]/g ));
排除匹配 1 2 3 4 let text = '张三:010-9999999;李四:020-9999999' console .log(text.match(/[^\d\-:;]+/g ))
原子表字符不解析 1 2 3 4 5 6 7 8 9 10 11 12 let text = '(jo-jo).+' console .log(text.match(/[()]/g ))console .log(text.match(/[.+]/g ))console .log(text.match(/[-]/g ))console .log(text.match(/[\-]/g ))console .log(text.match(/[j\-]/g ))console .log(text.match(/[a-z]/g ))
案例删除dom元素 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 let dom = ` <h1> jojo </h1> ` let dom2 = ` <H3> </H3> ` const reg = /<(h[1-6])>[\s\S]*<\/\1>/gi console .log(dom.replace(reg,'' ))console .log(dom2.replace(reg,'' ))
邮箱验证中原子组的使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 let test1 = '123131313@qq.com' ;let test2 = '123131313@sina.com.cn' ;let test3 = 'jojo-123@qq.com' ;console .log(test1.match(/^[\w-]+@\w+\.(com|cn|org)$/ ))console .log(test2.match(/^[\w-]+@\w+\.(com|cn|org)$/ ))console .log(test3.match(/^[\w-]+@\w+\.(com|cn|org)$/ ))console .log(test2.match(/^[\w-]+@(\w+\.)+(com|cn|org)$/ ))console .log(test1.match(/^[\w-]+@(\w+\.)+(com|cn|org)$/ ))console .log(test2.match(/^[\w-]+@(\w+\.)+(com|cn|org)$/ ))console .log(test3.match(/^[\w-]+@(\w+\.)+(com|cn|org)$/ ))
原子组引用完成替换操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 let text = ` <h1>jojo</h1> <span>jojolin</span> <h3>jojostoneocean</h3> ` ;let reg = /<(h[1-6])>([\s\S]+)<\/\1>/g ;console .log(text.match(reg));console .log(text.replace(reg, "<p>$2</p>" ));text.replace(reg,(p0,p1,p2,p3,p4 )=> { console .log(p0) console .log(p1) console .log(p2) })