The test() method is used to detect whether a string matches a pattern.
Returns a Boolean value indicating whether the given regular expression is matched in the string being looked up.
regexp.test(str)
parameter
regexp is required. A regular expression object containing a regular expression pattern or available flags.
str Required option. The string to be tested on it.
illustrate
The test method checks whether the string matches the given regular expression pattern, if so, returns true, otherwise returns false.
Each regular expression has a lastIndex attribute that records the position at which the last match ends.
var re = /^[1-9]{4,10}$/gi;var str = "123456";alert(re.test(str)); //Return true// After executing the above test, we can pop up
alert(re.lastIndex); // 6 pops up
That is, the last ended after the 6th character
Then the next time you call test, you will continue to search after the 6th character.
Workaround: Set the lastIndex property of the regular expression to 0
The specific code is as follows
<script type="text/javascript">var re = /^[1-9]{4,10}$/gi;var str = "123456";alert(re.test(str)); //Return truere.lastIndex=0;alert(re.test(str)); //Return true</script>JavaScript form validates the email mailbox, determines whether an input amount is email email, and implements it through regular expressions.
//Check email address
function check(){var email=document.getElementById("email").value;var isemail=/^[az]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[/.][az]{2,3}([/.][az]{2})?$/i;if (email=="") { alert("Please enter your email!"); return false; }if (email.length>25){ alert("Length too long"); return false}if (!isemail.test(email)){ alert("Not an email"); return false;}}The above brief discussion on the use of the test() function in js in the rules is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.