CKTIS / JavaScript
String.prototype.match — regular expression matches
The match() method finds regular expression matches in a string. The g flag changes the result shape: without it you get at most one match with capturing groups; with it you get an array of full matches. No match produces null.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
string.match(regexp)String.prototype.match finds pattern matches in a string without changing the string. For a standard RegExp object, the result is an array or null when nothing matches. Check for null before accessing result elements.
Without either g or y, the method returns the first match, if there is one. With y but not g, it tries to match starting exactly at the position obtained by applying ToLength to the regex’s current lastIndex. The method can therefore return a later match or return null even if the pattern matches elsewhere in the string. For example, /a/y with lastIndex set to -1 matches 'a' at index 0. For a successful match without g, element 0 holds the full match, and subsequent elements hold capturing groups. The index property gives the match position in UTF-16 code units, and input holds the searched string. With g, the array contains full, non-overlapping matches, but excludes capturing groups and that metadata. Without y, the search can continue past gaps between matches. With both g and y, matching stops as soon as the pattern fails at the current position. For example, 'a a'.match(/a/gy) returns only ['a'], even though another a occurs at index 2. To retain groups for multiple matches, use matchAll().
A string argument is interpreted as a regular expression pattern, not literal text: "abc".match(".")[0] produces "a". Omitting the argument or passing undefined creates an empty pattern, so the result is an empty-string match, not null.
Technically, the method delegates to Symbol.match if the argument provides that method. A custom implementation can therefore return a different result type; the array behavior above describes standard RegExp behavior.
Examples
const text = "item-12 item-34";
const first = text.match(/item-(\d+)/);
console.log(JSON.stringify([first[0], first[1], first.index]));
console.log(JSON.stringify(text.match(/item-(\d+)/g)));
console.log(text.match(/missing/g));Result
["item-12","12",0] ["item-12","item-34"] null
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.