JavaScript RegExp.prototype.test() Method
The test() method checks whether a regular expression matches a string, returning true or false.
Description
The test() method runs a search for a match between the regular expression and a string, returning true or false. It's the simplest way to validate a format, such as a postcode.
With the g or y flag, the regex remembers where the last match ended in lastIndex, so calling test() repeatedly with the same regex can alternate between true and false. Leave those flags off for simple checks.
It's often used for form validation, but remember to anchor the pattern with ^ and $ when the whole value must match; without them, /\d{4}/.test('abc12345') is true because part of the string matches.
Syntax
regexp.test(string)Parameters
- stringThe text to search.
Return Value
true if there is a match, otherwise false.
Example
Browser Support
The following information will show you the current browser support for the RegExp.prototype.test() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





