JavaScript String match() method
match a string against a regular expression

match() method matches a string against a regular expression.

syntax

match(regexp)

regexp A regular expression object, or any object that has a Symbol.match method.

If regexp is not a RegExp object and does not have a Symbol.match method, it is implicitly converted to a RegExp.

match() method returns an array with the matches. One item for each match.

match() method returns null if no match is found.

If the search value is a string, it is converted to a regular expression.

If no search value is given, it will return an array with an empty string [""]

If the regular expression has no flags (or is a string) only the first occurrence will be returned.

If the regular expression includes the g flag, all results matching the complete regular expression will be returned, but capturing groups are not included.

If the regular expression does NOT include the g flag, (or is a string) only the first match will be returned along with the capturing groups.

The returned item will have the following additional properties ↴

index The index of search where the result was found.

input A copy of the search string.

groups An object of named capturing groups having keys as the names and values as the captured matches.

let myString = 'Hello';

myString.match(/e/); returns ↴

['e', index: 1, input: 'Hello', groups: undefined]

syntax description
match myString against a regular expression or string
myString.match(regexp)