Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use test function in js regular expression

Shulou Source: shulou.com Published: 2022-06-02 03:46:07 10月03日 Update

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:

Tags: Content regular expression legal address legitimacy character string input function information function method user article detection code value interest name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MySQL MariaDB Linux Shulou Information