正则常用
Posted 爱吃鱼的猫#
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了正则常用相关的知识,希望对你有一定的参考价值。
正则表达式 let reg = /s/;//匹配非空白字符 let str = "a " ; console.log(reg.test(str)); let reg1 = /d/;//匹配0-9 console.log(reg.test(str)); let reg2 = /D/;//匹配非(0-9) console.log(reg.test(str)); let reg3 = /[\d\D]/;//匹配全部 let str1 = "123"; console.log(reg3.test(str1)); let reg4 = /[\w\W]/;//匹配全部 let str2 = "sadaaf"; console.log(reg4.test(str2)); let reg5 = /\w/;//匹配数字字母下划线 console.log(reg5.test(str2)); let reg6 = /\W/;//匹配非数字字母下划线 console.log(reg6.test(str2)); let reg7 = /\b/; console.log(reg7.test(str2)); let reg8 = /[a-zA-Z]/;//全部都是字母 console.log(reg8.test(str2)); let reg9 = /^0-9/;//全部都是数字 console.log(reg9.test(str2)); let str4 = "退出";//匹配汉字 let reg11 = /^[\u4E00-\u9FA5]{2,4}$/; console.log(reg11.test(str4)); let str5 = ‘ababab‘; let reg12 = /b{3}/;//匹配3个b console.log(reg12.test(str5)); let reg13 = /a*/;//匹配至少一个a console.log(reg13.test(str5)); let reg14 = /a\?/;//匹配a? console.log(reg14.test(str5)); 匹配手机号 let reg1 = /^1[3584]\d{9}$/ let str1 = "13234567823"; console.log(reg1.test(str1)); 正整数和0 以0开头 let reg = /^\d+$/; let str = "01"; let str7 = "1234"; console.log(reg.test(str)); console.log(reg.test(str7)); 不以 0 开头 let reg2 = /^([1-9]\d*)|(0)$/; let str2 = "11"; let str8 = "1"; let str3 = "-12"; let str4 = "0"; let str5 = "123"; let str6 = "0123456"; console.log(reg2.test(str2)); console.log(reg2.test(str3)); console.log(reg2.test(str4)); console.log(reg2.test(str5)); console.log(reg2.test(str6)); 小数 let reg3 = /^\d+\.\d+$/; console.log(reg3.test("0.2")); console.log(reg3.test("123.2")); console.log(reg3.test("123.0")); console.log(reg3.test("03.4")); console.log(reg3.test("0123.2")); 小数 let reg4 = /^(0|[1-9]\d*)\.\d*[1-9]$/; console.log(reg4.test("0.00")); console.log(reg4.test("02.1")); console.log(reg4.test("0.9")); console.log(reg4.test("123.90")); console.log(reg4.test("10.9")); 整数 let reg4 = /^-?\d+$/; console.log(reg4.test("0.9")); console.log(reg4.test("0")); console.log(reg4.test("1")); console.log(reg4.test("-10")); console.log(reg4.test("01")); 整数 let reg5 = /^-?(0|([1-9]\d*))$/ console.log(reg5.test("0")); console.log(reg5.test("01")); console.log(reg5.test("193")); 6-20位密码,必须以字母开头,数字字母下划线都可以 let reg = /^[A-Za-z]\w{5,19}$/ console.log(reg.test("0dasdfssghsfjgdghjh")); console.log(reg.test("a11111")); console.log(reg.test("a_1111")); console.log(reg.test("a1111")); console.log(reg.test("a11111aaaaaaaaaaaaaa")); console.log(reg.test("a11111aaaaaaaaaaaaaa_")); 邮箱 let reg = /^\[email protected]\w+(\.\w+){1,}$/ let reg = /^\[email protected]\w+(\.\w+)+$/ console.log(reg.test("[email protected]")); console.log(reg.test("[email protected]")); console.log(reg.test("[email protected]")); console.log(reg.test("[email protected]")); let str = "Mr Blue has a blue car and blue house"; let reg = /blue/; // console.log(str.search(reg));//找到第一个blue的下标 let reg1 = /blue/i;//i表示不区分大小写 console.log(str.search(reg1));//找到第一个不区分大小写的blue的下标 let reg2 = /blue/gi;//i表示不区分大小写,g表示全局查找 console.log(str.search(reg2));//全局查找,找到第一个不区分大小写的blue的下标 let reg3 = /blue/gi;// console.log(str.match(reg3));//全局查找,不区分大小写的所有blue let str = "Blue has a blue car and blue house"; let reg = /blue/; console.log(str.search(reg));//查找下标 console.log(str.match(reg));//查找不仅仅是下标,跟exec()类似,包括字符串,下标index以及来源input console.log(reg.exec(str));//查找字符串,下标index,来源input console.log(str.replace(reg,‘red‘));//替换
以上是关于正则常用的主要内容,如果未能解决你的问题,请参考以下文章