String

String.prototype.match

Retrieves the result of matching a string against a regular expression

Syntax

JavaScript
string.match(regexp)

Parameters

ParameterTypeDescription
regexpRegExpA regular expression object

Return Value

An Array with matches, or null if no match found

Examples

Basic Usage
const str = 'The rain in Spain';
const result = str.match(/ain/g);
console.log(result); // ['ain', 'ain']
Practical Example
const email = 'Contact us at [email protected] or [email protected]';
const emails = email.match(/[\w.]+@[\w.]+/g);
console.log(emails); // ['[email protected]', '[email protected]']
Advanced Usage
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

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.