Regular Expression Object
The test method returns a Boolean value that indicates whether or not a pattern exists in a searched string.
Syntax
rgexp.test(str)
The test method syntax has these parts:
Part |
Description |
rgexp |
Required. A Regular Expression object. Can be a variable name or a literal. |
str |
Required. The string to test a search on. |
Return value
Returns a Boolean value that indicates whether or not a pattern exists in a searched string.
Remarks
The test method checks to see if a pattern exists within a string and returns true if so, and false otherwise.
The RegExp object is not modified by the test method.
The following example illustrates the use of the test method:
function TestDemo(re, s)
{
var s1;
// Test string for existence of regular expression.
if (re.test(s))
s1 = " contains ";
else
s1 = " does not contain ";
// Get text of the regular expression itself.
return(s + s1 + re.source);
}
document.write(TestDemo(new RegExp("Was"),"Washington"));
|
To run the code, paste it into JavaScript Editor, and click the Execute button.
See Also: RegExp Object, Regular Expression Object Methods, Regular Expression Object Properties, Regular Expression Syntax |