Skip to content

extract

The extract() method returns details about the first match of a pattern in a string, or undefined when there is no match.

Interactive example

Syntax

Classic signature

typescript
function extract<
	GenericInput extends string
>(
	input: GenericInput, 
	pattern: string | RegExp
): ExtractResult | undefined;

Curried signature

typescript
function extract<
	GenericInput extends string
>(
	pattern: string | RegExp
): (input: GenericInput) => ExtractResult | undefined;

Parameters

  • input: The string to search in.
  • pattern: The search pattern, which can be a string or a regular expression.

Return value

An ExtractResult object when a match is found, or undefined when there is no match.

ExtractResult contains:

  • matchedValue: The full matched substring.
  • groups: An array of captured groups (excluding the full match).
  • namedGroups: A record of named groups if present.
  • offset: The start index of the match.
  • self: The original input string.

See also

  • extractAll: Extracts details about all matches.
  • match: Searches for a match with a regular expression.
  • matchAll: Searches for all matches with a regular expression.

Sources

Released under the MIT license.