正则表达式,又称“正则表达式”,是我们自己编写的“规则”,专门用来检测一个字符串是否符合“规则”。我们使用一些特殊的字符或符号来定义一个“规则公式”,然后使用我们定义的“规则公式”来检查字符串是否合格varreg=/\d+/varstr1='123'varstr2='abc'console.log(reg.test(str1))//trueconsole。log(reg.test(str2))//false上面的变量reg是当自定义规则检测到字符串str1,当字符串符合规则检测到字符串str2,不符合规则时,创建aregularexpressiontoformulate""Rules"必须按照别人要求的方式来制定。在//中间写一些字母和符号,称为正则表达式,比如/abcdefg/正则表达式有两种创建方式:文字和constructorstocreateliteralsCreate//下面是创建正则表达式的字面值varreg=/abcdefg/这个正则表达式可以检测字符串ConstructorCreate//下面是创建正则表达式的构造函数varreg=newRegExp('abcdefg')console.log(reg)///abcdefg/是使用构造函数方法和字面量值创建的,结果是一样的下面说说正则表达式中涉及的一些符号。元字符。:匹配任何不是的字符一个换行符\:Translatesymbols,将有意义的符号转化为无意义的字符,将无意义的字符转化为有意义的符号Symbols\s:匹配空白字符(space/tab/...)\S:匹配非空白字符\d:匹配数字\D:匹配非数字\w:匹配数字和字母下划线\W:匹配非数字字母有了下划线和元字符,我们可以简单地制定一些规则varreg=/\s/varstr='ab'varstr2='ab'console.log(reg.test(str))//trueconsole.log(reg.test(str2))//falsevarreg=/\d/varstr='abc1'varstr2='abc'console.log(reg.test(str))//trueconsole.log(reg.test(str2))//falsevarreg=/\w/varstr='a1'varstr2='#@$'console.log(reg.test(str))//trueconsole.log(reg.test(str2))//false限定符*:前面的内容至少重复0次,即可以出现0到正无限次+:前面的内容重复至少1次,也就是可以出现1到正无限次?:前面的内容可以重复0次或者1次,也就是可以出现0到1次{n}:前面的内容可以重复n次,也就是必须出现n次{n,}:在A之前内容至少出现n次,即出现n~正无穷次{n,m}:前面的内容至少出现n次,最多出现m次,即出现n~m次。限定符与元字符结合使用//下面的正则化表示验证数可以出现0到正无限次varreg=/\d*/varstr='abc'varstr2='abc1'varstr3='abc123'console.log(reg.test(str))//trueconsole.log(reg.test(str2))//trueconsole.log(reg.test(str3))//true//下面的正则表示验证数字可以出现1到正无限次varreg=/\d+/varstr='abc'varstr2='abc1'varstr3='abc123'console.log(reg.test(str))//falseconsole.log(reg.test(str2))//trueconsole.log(reg.test(str3))//true//下面的regex表示验证号可以出现0~1次varreg=/\d?/varstr='abc'varstr2='abc1'console.log(reg.test(str))//trueconsole.log(reg.test(str2))//true//下面的正则表示验证号必须出现3次变量例如=/\d{3}/varstr='abc'varstr2='abc1'varstr3='abc123'console.log(reg.test(str))//falseconsole.log(reg.test(str2))//falseconsole.log(reg.test(str3))//true//下面的规律表示验证数出现3到正无限次varreg=/\d{3,}/varstr='abc'varstr2='abc1'varstr3='abc123'varstr4='abcd1234567'console.log(reg.test(str))//falseconsole.log(reg.test(str2))//falseconsole.log(reg.test(str3))//trueconsole.log(reg.test(str4))//true//下面的regex表示验证号只能出现3到5次varreg=/\d{3,5}/varstr='abc'varstr2='abc1'varstr3='abc123'varstr4='abc12345'console.log(reg.test(str))//falseconsole.log(reg.test(str2))//falseconsole.log(reg.test(str3))//trueconsole.log(reg.test(str4))//真定界符^:表示开始$:表示结束定界符是为了限制字符串的开始和结束//以下表示从头到尾theend只能是数字,出现3到5次varreg=/^\d{3,5}$/varstr='abc'varstr2='abc123'varstr3='1'varstr4='1234567'varstr5='123'varstr6='12345'console.log(reg.test(str))//falseconsole.log(reg.test(str2))//falseconsole.log(reg.test(str3))//falseconsole.log(reg.test(str4))//falseconsole.log(reg.test(str5))//trueconsole.log(reg.test(str6))//true特殊符号():限定一组元素[]:字符集,表示[]中的任何一个都可以[^]:反向字符集,表示[^]中的任何一个都可以接受-:范围,如a-z表示从字母a到字母z可以是|:or,or正则表达式中的a|b表示可以使用字母a或者b现在我们可以组合几个符号来使用//下面是一个简单的邮箱验证//不以_$开头,任意字符至少出现6次,一个@符号,(163|126|qq|sina)任意一个,一个点,(com|cn|net)任意一个varreg=/^[^_$].{6,}@(163|126|qq|sina)\.(com|cn|net)$/标识符i:表示忽略大小写。这个i写在正则的末尾/\w/i表示匹配正则表达式时不区分大小写g:表示全局匹配。这个g写在正则表达式的末尾/\w/g是全局匹配字母数字下划线正则表达式的方法。正则表达式提供了一些方法给我们测试测试用来检测和捕获字符串中的内容。用来检测字符串是否符合我们的正则语法:regular.test(string)返回值:booleanconsole.log(/\d+/.test('123'))//trueconsole.log(/\d+/.test('abc'))//falseexecexec是抓取字符串中符合条件的内容数组varreg=/\d{3}/varstr='hello123world456Hello789'varres=reg.exec(str)console.log(res)/*["123",index:5,input:"hello123world456Hello789",groups:undefined]0:"123"groups:undefinedindex:5input:"hello123world456hello789"length:1__proto__:Array(0)*/数组中的第0项是匹配字符串的内容。index属性表示匹配字符串的字符串的索引Stringmethods字符串中有一些方法也可以与正则表达式一起使用。search搜索就是查找字符串中是否有满足正则条件的内容。return-1varreg=/\d{3}/varstr='hello123'varstr2='hello'console.log(str.search(reg))//5console.log(str2.search(reg))//-1matchmatch查找字符串中满足正则条件的内容返回语法:string.match(regular)返回值:当没有标识符g时,同exec方法。当有标识符g时,返回一个数组,即匹配的每一项varreg=/\d{3}/varstr='hello123world456'varstr2='hello'console.log(str.match(reg))//["123",index:5,input:"hello123wor456",groups:undefined]console.log(str2.match(reg))//nullvarreg=/\d{3}/gvarstr='hello123world456'varstr2='hello'console.log(str.match(reg))//["123","456"]console.log(str2.match(reg))//nullreplacereplace是替换字符串中的字符串满足正则条件语法:string.replace(Regular,stringtobereplaced)返回值:替换字符串varreg=/\d{3}/varstr='hello123world456'varstr2='hello'console.log(str。代替(reg))//hello666world456console.log(str2.replace(reg))//hellovarreg=/\d{3}/gvarstr='hello123world456'varstr2='hello'console.log(str.replace(reg))//hello666world666console.log(str2.replace(reg))//你好
