String .match() / matchAll() / search()
| Since: | ES3(ECMAScript 1999) |
|---|
Use string methods to search with regular expressions. match() and matchAll() return the matched results, while search() returns the position of the match.
Syntax
// Returns information about the pattern match. var result = "string".match(regexp); // Returns an iterator of all matches with full details. var results = "string".matchAll(regexp); // Returns the index of the first match. var index = "string".search(regexp);
Method List
| Method | Description |
|---|---|
| string.match(regexp) | Returns the match results as an array. Without the g flag, it returns the first match along with any capture groups. With the g flag, it returns all matches as an array. Returns null if no match is found. |
| string.matchAll(regexp) | Returns all matches with detailed information using a g-flagged regular expression. Since capture group contents are included, you get more detail than using match() with the g flag. |
| string.search(regexp) | Returns the index of the first match as a number. Returns -1 if no match is found. |
Sample Code
sample_stringMatch.js
// Basic usage of match()
var text = "The phone number is 090-1234-5678";
var result = text.match(/\d{2,4}-\d{4}-\d{4}/);
console.log(result[0]); // Outputs '090-1234-5678'.
// Use match() with the g flag to get all matches.
var colors = "red blue green";
var matches = colors.match(/[a-z]+/g);
console.log(matches); // Outputs '["red", "blue", "green"]'.
// Using match() with capture groups
var dateText = "Today is 2026-03-15";
var dateResult = dateText.match(/(\d{4})-(\d{2})-(\d{2})/);
console.log(dateResult[0]); // Outputs '2026-03-15'. The full matched string.
console.log(dateResult[1]); // Outputs '2026'. The first capture group.
console.log(dateResult[2]); // Outputs '03'. The second capture group.
console.log(dateResult[3]); // Outputs '15'. The third capture group.
// Use matchAll() with the g flag to get capture groups for every match.
var log = "Error: E001, Warning: E002, Error: E003";
var iterator = log.matchAll(/E(\d{3})/g);
var entry = iterator.next();
while (!entry.done) {
console.log(entry.value[0] + " -> Code: " + entry.value[1]); // Gets both the full match and the capture group.
entry = iterator.next();
}
// Use search() to get the position of a match.
var sentence = "JavaScript is a fun language";
console.log(sentence.search(/fun/)); // Outputs '16'. The starting index of the match.
console.log(sentence.search(/Python/)); // Outputs '-1'. No match was found.
// Input validation: Check whether an email address is in a valid format.
var userInput = "iori@yagami-clan.jp";
var emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (userInput.match(emailPattern)) {
console.log("Valid email address"); // Outputs 'Valid email address'.
}
// Extract all URLs from a block of text.
var article = "Visit https://kof.snk.com and https://snk.com for more info.";
var urls = article.match(/https?:\/\/[^\s]+/g);
console.log(urls); // Outputs '["https://kof.snk.com", "https://snk.com"]'.
090-1234-5678 [ 'red', 'blue', 'green' ] 2026-03-15 2026 03 15 E001 -> Code: 001 E002 -> Code: 002 E003 -> Code: 003 16 -1 Valid email address [ 'https://kof.snk.com', 'https://snk.com' ]
Overview
string.match() is a string method that returns regex match results as an array. Its behavior differs significantly based on the g flag: without the g flag, it returns detailed information including capture group contents; with the g flag, it returns only an array of matched strings, and capture group information is lost.
string.matchAll() retrieves all matches with capture group information using a g-flagged regular expression. Since it provides all capture group contents that match() with the g flag cannot, it is useful when you need to extract individual data from multiple matches.
string.search() is a lightweight method that returns only the position of the match, making it suitable when you just need to know whether something matches and where. To search using methods on the regular expression object itself, see regexp.test() / exec().
Browser Compatibility
3 or earlier ×
3 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.