Explain the test function of JS regular expression

Time:2020-10-27

Function introduction: the return value of this method is a Boolean value. Through this value, you can match whether there is a regular expression matching result in the string. If there is a matching content, it returns true. If there is no matching content, it returns false. This method is commonly used to judge the validity of user input data, such as checking the validity of email

Basic grammar: objReg.test (objStr)
Objreg required regexp object name
Objstr string to match detect
Explain the example code:

Copy codeThe code is as follows:
<html>
<script language=”javascript” type=”text/javascript”>
/*Designed By Androidyue*/
/*
Function: check the validity of email address
*/
function checkEmail(){
//Get the email information entered by the user in the text box
var objStr=document.getElementById(“email”).value;
//Set regular expression matching email
var objReg=//w+[@]{1}/w+[.]/w+/;
//document.write(objStr);
//If there is a matching content in the string, if there is a prompt correct information, otherwise an error is returned
if(objReg.test(objStr)){
Alert (“the email address is legal!”);
}else{
Alert (“the email address is illegal!”);
}
}
</script>
<body>
Please enter email address:
< input type = “text” > <! — set email input box — >
< input type = “button” value = detect validity “onclick =” checkemail() “> <! — set button to start to detect email legality events — >
</body>
</html>