Definition and Usage:
The test () method is used to detect whether a string matches a pattern.
Grammar:
Regexpobject.test (string);
Regexpobject: Regular expression;
String: Must be a parameter, the character to be detected;
return value:
If string: string contains text that matches the regular expression: Regexpobject, returns True, otherwise false;
Description
Call the test () method of the RegExp object R and pass the string s to it, which is equivalent to this expression: (R.exec (s)! = null).
Usage development:
1 //each regular expression has a LastIndex property that records where the last match ended, including the Exec method. 2 3 varRe =/^[1-9]\d{4,10}$/gi;4 varstr = "123456";5Alert (Re.test (str));//returns True6str = "1234567";7Alert (Re.test (str));//returns false8 9 //Workaround:Ten One varRe =/^[1-9]\d{4,10}$/gi; A varstr = "123456"; -Alert (Re.test (str));//returns True - the //after executing the above test, we can eject the -alert (Re.lastindex);//pop up 6 - //That means that the last time after the 6th character ends - //then the next time you call test, the search will continue after the 6th character. + - //workaround, set the LastIndex property of the regular expression to 0 + A //The specific code is as follows: at<script type= "Text/javascript" > - varRe =/^[1-9]\d{4,10}$/gi; - varstr = "123456"; -Alert (Re.test (str));//returns True -str = "1234567"; -Re.lastindex=0; inAlert (Re.test (str));//returns True -</script> to + //or: - the //Regular mode to remove the global mode, the G is removed on the line, G is to continue down the meaning, not necessarily do not add g, change to: var re =/^[1-9]\d{4,10}$/i; * varstr = "123456"; $Alert (Re.test (str));//returns TruePanax Notoginsengstr = "1234567"; -Alert (Re.test (str));//returns True
Code
JavaScript Test () method