RegExp .test() / exec()
| Since: | ES3(ECMAScript 1999) |
|---|
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
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
For detailed information on regular expression syntax, refer to the MDN documentation or other references.
| 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
var postalPattern = /^\d{3}-\d{4}$/; // Defines a postal code format.
console.log(postalPattern.test("123-4567")); // Outputs 'true'.
console.log(postalPattern.test("abc-defg")); // Outputs 'false'.
console.log(postalPattern.test("12-345")); // Outputs 'false'.
// Pattern 2: practical use in a validation function
function isValidEmail(email) {
var emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return emailPattern.test(email);
}
console.log(isValidEmail("okabe_rintaro@wp-p.info")); // Outputs 'true'.
console.log(isValidEmail("invalid-email")); // Outputs 'false'.
console.log(isValidEmail("makise@kurisu.c")); // Outputs 'false'. Invalid because the TLD is only one character.
// Pattern 3: case-insensitive search using the i flag
var namePattern = /kurisu/i; // The i flag ignores uppercase/lowercase differences.
console.log(namePattern.test("Makise Kurisu")); // Outputs 'true'.
console.log(namePattern.test("makise kurisu")); // Outputs 'true'.
console.log(namePattern.test("Makise Okabe")); // Outputs 'false'.
// Pattern 4: retrieving detailed match information with exec()
var emailPattern = /([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/;
var result = emailPattern.exec("Contact: okabe_rintaro@wp-p.info");
console.log(result[0]); // Outputs 'okabe_rintaro@wp-p.info'. The entire matched string.
console.log(result[1]); // Outputs 'okabe_rintaro'. The first capture group.
console.log(result[2]); // Outputs 'wp-p.info'. 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'.
true false false true false false true true false okabe_rintaro@wp-p.info okabe_rintaro wp-p.info 9 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.