Home  >  Article  >  Web Front-end  >  JavaScript study notes (5) Regular expressions_basic knowledge

JavaScript study notes (5) Regular expressions_basic knowledge

WBOY
WBOYOriginal
2016-05-16 18:07:591003browse

Commonly used metacharacters are:
•. Finds a single character, except newlines and line terminators;
•w matches letters, Chinese characters, numbers, underscores and other symbols;
•s matches whitespace characters (including spaces) , tab characters, etc.);
•d matches numbers;
•b matches at the beginning or end of a word;
Commonly used quantifiers are:
•^n matches anything starting with n String;
•n$ matches any string ending in n;
•n matches any string containing at least one n;
•n* matches any character containing zero or more n String;
•n? matches any string containing zero or one n;
•n{X} matches a string containing a sequence of X n;
•n{X, Y} matches A string containing X or Y n sequences;
A simple example, mainly used to verify mobile phone numbers, phone numbers and emails:
Javascript part code:

Copy code The code is as follows:

function isMobile() {
var mobile = document.getElementById("mobile_phone");
var num = mobile.value;
var reg = /^(13[0-9]|186|188|150|151|158|159|147)d{8}$/;
if(num == "") {
alert("Please enter your complete mobile phone number");
mobile.focus();
return false;
} else if (reg.test(num)) {
alert("The format of the entered mobile phone number is correct");
} else {
alert("Please enter the correct 11-digit mobile phone number");
mobile.focus();
return false;
}
}
function isEmail() {
var email = document.getElementById("email");
var email_value = email.value;
if(email_value = = "") {
alert("Please enter your complete email address");
email.focus();
return false;
} else {
var reg = /^[a -zA-Z0-9](w) @(w) (.) (com|com.cn|net|cn|net.cn|org|biz|info|gov|gov.cn|edu|edu.cn) $/;
if(reg.test(email_value)) {
alert("The email format entered is correct");
} else {
alert("Please enter the correct email format") ;
email.focus();
return false;
}
}
}
function isPhone() {
var phone = document.getElementById("phone") ;
var phone_value = phone.value;
if(phone_value == "") {
alert("Please enter the complete landline number");
phone.focus();
return false;
} else {
var reg = /^[(]?0d{2,3}[)]?s*[-]?s*d{7,8}$/; // 010-87989898 01098989898 (0712)8989898 010 - 23343434 The landline numbers in these formats all meet the requirements
if(reg.test(phone_value)) {
alert("The entered landline number is correct");
} else {
alert("The entered landline number has an incorrect format");
phone.focus();
return false;
}
}
}

HTML part code:
Copy code The code is as follows:













Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn