一、语法
1、普通字符
包括所有大写和小写字母、所有数字、所有标点符号和一些其他符号。
const flag = 'People have to live,Love has its9own charm_'
const str1 = /a/
const str2 = /,/
const str3 = /L/
const str4 = /_/
console.log('字符', flag)
console.log('匹配小写', flag.search(str1))
console.log('匹配标点', flag.search(str2))
console.log('匹配大写', flag.search(str3))
console.log('匹配其他', flag.search(str4))
注:
/^P/:匹配除了P以外的字符
[A-Z] 表示一个区间,匹配所有大写字母,[a-z] 表示所有小写字母。
\s 是匹配所有空白符,包括换行,\S 非空白符,包括换行。
匹配字母、数字、下划线。等价于 [A-Za-z0-9_]
二、修饰符
三、元字符
四、优先级
五、匹配位置
1、匹配开头^匹配结尾$
const flag = '111@qq.com'
const str1 = /^1/
const str2 = /com$/
const str3 = /^1.*com$/
console.log('flag', flag)
console.log('匹配开头是1', str1.test(flag))
console.log('匹配结尾是com', str2.test(flag))
console.log('匹配开头是1且结尾是com', str3.test(flag))
2、匹配字符前?=p和字符后?!p
const flag = '111@qq.com'
const str1 = /qq(?!@)/
const str2 = /qq(?=.)/
console.log('flag', flag)
console.log('匹配@后是qq', str1.test(flag))
console.log('匹配.前面是qq', str2.test(flag))
六、常用
1、Email地址:/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})
/
2
、
电
话
号
码
:
/
[
1
]
[
3
,
4
,
5
,
7
,
8
]
[
0
−
9
]
9
/ 2、电话号码:/^[1][3,4,5,7,8][0-9]{9}
/2、电话号码:/[1][3,4,5,7,8][0−9]9/
3、身份证号(15位、18位数字),最后一位是校验位,可能为数字或字符X:(\d{15}$)|(^\d{18}$)|(\d{17}(\d|X|x)KaTeX parse error: Undefined control sequence: \d at position 11: ) 4、日期格式:^\̲d̲{4}-\d{1,2}-\d{…
一个月的31天(01~09和1~31):^((0?[1-9])|((1|2)[0-9])|30|31)$
5、验证输入只能是汉字 :’ ^’+’[\u4e00-\u9fa5]{0,}$’
七、参考
1、https://www.runoob.com/regexp/regexp-rule.html
2、https://www.cnblogs.com/meowv/p/12874812.html