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
| Method | Description |
|---|---|
| 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
| Pattern | Description |
|---|---|
| ^ | 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. |
| \d | Matches a single digit. Equivalent to [0-9]. |
| \w | Matches 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
3 or earlier ×
4 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.