1. Realize regular expression mobile phone verification.
Common mobile phone numbers are all 11 digits
The first 3 digits indicate the region and the operator
Regular expression ^1 means beginning with 1
p>
(3[0-9]) means that the second number is 3, and the data following 3 is a number from 0-9 so here are two numbers
\d{ 8} Followed by 8 digits
Because there are Unicom number segments, mobile number segments, and telecom number segments, the |or operator is used.
If you don’t understand, you can abbreviate it as let reg=/^[1][3,4,5,7,8,9][0-9]{9}$/;
< p> ^[1] The number starting with 1, [3,4,5,7,8,9], the second number is one of the set, [0-9], the range of 0 to 9, {9 } Match 9 times, $ ends.
function isPhoneNumber(phoneNum){
let reg=/^1(3[0-9]|4[5,7]|5[0,1,2,3,4,5,6,7,8,9 ]|6[2,5,6,7]|7[0,1,7,8]|8[0-9]|9[1,8,9])\d{8}$/;
return reg.test(phoneNum);
}
console.log(isPhoneNumber(18212345678));
2. Realize regular expression email verification
Simple implementation
function isEmail(email){
let reg=/^\[email protected][a-z0-9]+\.[az]{2,4}$/ ;
return reg.test(email);
}
console.log(isEmail("[email protected]"));
Complex implementation
Regular expression reg[a -zA-Z0-9_.-] Indicates that the first range attention point at the beginning includes underscore, decimal point, and minus sign.
function isPhoneNumber(phoneNum){
let reg=/^1(3[0-9]|4[5,7]|5[0,1,2,3,4,5,6,7,8,9 ]|6[2,5,6,7]|7[0,1,7,8]|8[0-9]|9[1,8,9])\d{8}$/;
return reg.test(phoneNum);
}
console.log(isPhoneNumber(18212345678));
function< span style="color: #000000;"> isEmail(email){
let reg=/^\[email protected][a-z0-9]+\.[az]{2,4}$/ ;
return reg.test(email);
}
console.log(isEmail("[email protected]"));
let reg=/^[a-zA-Z0-9_.-] [email protected][a-zA-Z0-9-]+(\.[a -zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;