Test method
Returns a Boolean value that indicates whether the pattern exists in the string being searched.
Rgexp.test (str)
JavaScript regular object creation and usage
declaring JavaScript regular expressions
The code is as follows |
Copy Code |
var recat = new RegExp ("cat"); You can also var recat =/cat/; Perl Style (recommended) |
Learn the most commonly used test exec match search replace split 6 methods
Test checks whether the specified string exists
The code is as follows |
Copy Code |
var data = "123123"; var recat =/123/gi; Alert (recat.test (data)); True
|
Check to see if the character is present G continue to go down I case insensitive
Instance
Example
The following example illustrates the use of the test method:
The code is as follows |
Copy Code |
<script language=jscript> function Testdemo () { try{ var re= "abc" var s=/a/ This sets the regular expression Alert (S.test (RE)) Returns True if it is included }catch (e) { Alert ("Err") }} Testdemo () </script> |
Example
The code is as follows |
Copy Code |
<script language= "JavaScript" > function Checkmobile (s) ... { var Regu =/^[1][3][0-9] ... {9}$/; var re = new RegExp (Regu); return Re.test (s); } Checkmobile (' 13909910000 '); </script>
|
Purpose: Check whether the input mobile phone number is correct, if return true by validation, false
Other related introduction
Test (String): Tests whether a string contains a match result
Regular
method in a String object
Match pattern: a regular match based on pattern and, if matched to, returns a matching result, such as not matching return null
Search (pattern) : A regular match based on pattern and, if matched to a result, returns the number of its indexes; otherwise return-1
Replace (pattern,replacement): a regular match based on pattern to replace the result with a replacement
Split (pattern): a regular split based on pattern, returning an array of partitions