JavaScript test() Method
Example
Search a string for the character "e":
var str = "The best things in life are free";
var patt = new RegExp("e");
var res = patt.test(str);
Try it Yourself »
Definition and Usage
The test() method tests for a match in a string.
This method returns true if it finds a match, otherwise it returns false.
Browser Support
Method | |||||
---|---|---|---|---|---|
test() | Yes | Yes | Yes | Yes | Yes |
Syntax
RegExpObject.test(string)
Parameter Values
Parameter | Description |
---|---|
string | Required. The string to be searched |
Return Value
Type | Description |
---|---|
Boolean | Returns true if it finds a match, otherwise it returns false |
Technical Details
JavaScript Version: | ECMAScript 1 |
---|
More Examples
Example
Do a global search, and test for "Hello" and "W3Schools" in a string:
// The string:
var str = "Hello world!";
// Look for "Hello"
var patt = /Hello/g;
var result = patt.test(str);
// Look for "W3Schools"
patt2 = /W3Schools/g;
result2 = patt2.test(str);
Try it Yourself »
❮ JavaScript RegExp Object