How to use test function in js regular expression
This article mainly introduces how to use the test function in js regular expressions. It is very detailed and has a certain reference value. Friends who are interested must read it!
Function description: the return value of this method is a Boolean value, through which you can match whether the string exists in the regular expression matching result. If there is a match, return ture. If there is no match, return false. This method is often used to judge the validity of user input data, such as verifying the legitimacy of Email.
Basic syntax: objReg.test (objStr)
ObjReg required RegExp object name
The string to be matched by the objStr
Explain the example code:
/ * Designed By Androidyue*//* function: check the validity of the Email address * / function checkEmail () {/ / get the information var objStr=document.getElementById ("email") entered by the user in the text box. Value;// sets the regular expression var objReg=//w+ [@] {1} / w + [.] / xss_clean / [objStr) that matches Email. / / if you judge whether there is a match in the string, if there is a correct message, the error if (objReg.test (objStr)) {alert ("the Email address is legal!");} else {alert ("the Email address is illegal!");}} enter the Email address: