Favorites

JavaScript RegExp.prototype.test() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
RegExp.prototype.test()
JavaScript RegExp Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

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

const postcode = /^\d{4}$/;
console.log(postcode.test('5000'));
console.log(postcode.test('50000'));

const g = /a/g;
console.log(g.test('a'), g.test('a'));   // lastIndex trap

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee