//中括号表示指定范围
var str = "qa123_";
var reg = /^[a-z0-9A-Z_]{5,8}$/;
//\w代表数字母下划线
var regg = /^\w{5,8}$/;
//\d代表数字0-9
var regg = /^\d{5,8}$/;
//\s空格或换行
//?匹配0位或一位
//.匹配所有
//\转义
//一位到多位
var reggg = /^[0-9]{1,}$/;
var regggg = /^[0-9]+$/;
//test方法返回布尔值,exec返回匹配的内容
var result = regg.exec(str);
console.log(result);
//匹配邮箱格式
var input = "12w_d56@163.com"
var reg = /^\w{5,12}@163\.com$/;
if (reg.test(input)) {
console.log("pass")
} else {
console.log("fail")
}
//去掉字符串中的字母
var input = "123abc456def"
//最后+g全局匹配
var word = /[a-zA - Z]/g;
var result = input.replace(word, "")
console.log(result)
//截取字符串
var input = "2021-12-01"
//()用来分组
var reg = /^(\d{4})-(\d{2})-(\d{2})$/;
console.log(reg.exec(input))
result = reg.exec(input)
console.log(result[1]); //2021
console.log(result[2]); //12