Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. RegExp .test() / exec()

RegExp .test() / exec()

Methods of a regular expression object that determine whether a string matches a pattern. test() returns a boolean, while exec() returns detailed information about the match.

Syntax

// Checks whether the string matches the pattern.
var result = regexp.test("string");

// Retrieves detailed information about the match.
var match = regexp.exec("string");

Methods

MethodDescription
regexp.test("string")Returns true if the string matches the pattern, or false if it does not. Best suited for simple match checks.
regexp.exec("string")Returns an array with detailed information about the first match. Returns null if there is no match. You can also retrieve the contents of capture groups and the position of the match.

Common Regular Expression Patterns

PatternDescription
^Matches the beginning of the string.
$Matches the end of the string.
.Matches any single character.
*Matches zero or more repetitions of the preceding pattern.
+Matches one or more repetitions of the preceding pattern.
\dMatches a single digit. Equivalent to [0-9].
\wMatches a single alphanumeric character or underscore.
[abc]Matches any one of the characters a, b, or c.
(pattern)Groups a pattern. The matched portion can be retrieved as a capture group.

Sample Code

// Example: checking a match with test()
var pattern = /^\d{3}-\d{4}$/; // Defines a postal code format.
console.log(pattern.test("123-4567")); // Outputs 'true'.
console.log(pattern.test("abc-defg")); // Outputs 'false'.
console.log(pattern.test("12-345"));   // Outputs 'false'.

// Example: retrieving detailed match information with exec()
var emailPattern = /(\w+)@(\w+\.\w+)/;
var result = emailPattern.exec("Contact: taro@example.com");
console.log(result[0]); // Outputs 'taro@example.com'. The entire matched string.
console.log(result[1]); // Outputs 'taro'. The first capture group.
console.log(result[2]); // Outputs 'example.com'. The second capture group.
console.log(result.index); // Outputs '9'. The starting index of the match.

// When there is no match
var noMatch = emailPattern.exec("no email address here");
console.log(noMatch); // Outputs 'null'.

Details

regexp.test() is the simplest matching method, returning true or false to indicate whether a string matches the pattern. It is well suited for input validation and conditional branching when you only need to know whether a match exists.

regexp.exec() returns an array with detailed information about the matched string. Index [0] holds the entire matched string, and [1] onward holds the contents of each capture group. The index property gives you the position of the match, making it ideal for parsing strings and extracting data.

Regular expression patterns are written enclosed in slashes /. Adding the g flag at the end enables a global search, and the i flag makes the search case-insensitive. To use regular expressions with string methods, see string.match() / matchAll() / search().

Browser Compatibility

Chrome Chrome
49+
Supported in all versions
Firefox Firefox
57+
Supported in all versions
Safari Safari
18+
Supported in all versions
Edge Edge
80+
11 or earlier ×
IE IE
11+
3 or earlier ×
Opera Opera
48+
4 or earlier ×
iOS Safari iOS Safari
18+
Supported in all versions
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .