Executes the search for a match between a regular expression and a specified string. Returns true or false.
Method of RegExp
Syntax
regexp.test( [str] )
Parameters
Parameter | Description |
---|---|
regexp | The name of the regular expression. It can be a variable name or a literal. |
str | The string against which to match the regular expression. If omitted, the value of RegExp.input is used. |
Description
When you want to know whether a pattern is found in a string use the test method (similar to the String.search method); for more information (but slower execution) use the RegExp.exec method (similar to the String.match method).
Example
The following example returns a message which depends on the success of the test: