String.prototype.match
Retrieves the result of matching a string against a regular expression
Syntax
string.match(regexp)Parameters
| Parameter | Type | Description |
|---|---|---|
| regexp | RegExp | A regular expression object |
Return Value
An Array with matches, or null if no match found
Examples
const str = 'The rain in Spain';
const result = str.match(/ain/g);
console.log(result); // ['ain', 'ain']const email = 'Contact us at [email protected] or [email protected]';
const emails = email.match(/[\w.]+@[\w.]+/g);
console.log(emails); // ['[email protected]', '[email protected]']const str = 'Date: 2024-01-15';
const match = str.match(/(\d{4})-(\d{2})-(\d{2})/);
if (match) {
console.log(match[1]); // '2024'
}Understanding String.prototype.match
The String.prototype.match method in JavaScript retrieves the result of matching a string against a regular expression. It belongs to the String object and is one of the most widely used methods for working with string values in modern JavaScript and TypeScript applications.
The method signature is string.match(regexp). It accepts 1 parameter: regexp. When called, it returns an array with matches, or null if no match found. Understanding when and how to use match() helps you write more expressive, readable code.
Common use cases for String.prototype.match include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like string-matchall, string-search, string-replace, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for String.prototype.match is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
String.prototype.matchAllReturns an iterator of all results matching a string against a regular expression, including capturing groups
String.prototype.searchExecutes a search for a match between a regular expression and this string, returning the index of the first match
String.prototype.replaceReturns a new string with one or all matches of a pattern replaced by a replacement
RegExp.prototype.execExecutes a search for a match in a specified string and returns a result array or null
More String Methods
Other methods in the String object
Related Tools
More String Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.