JS: RegExp.prototype.exec

By Xah Lee. Date: . Last updated: .
regex.exec(string)
Match regex pattern in string string.

If there is capture in regex, return a array of captured groups, or null if no match.

If there is no capture in regex, return a array of 1 item that's the first occurrence, or null if no match.

If there is a RegExp Flag global, repeated call will start match at RegExp.prototype.lastIndex. (the end position of last found match) This allows you to find all occurrences in a loop. 💡 TIP: better use String.prototype.matchAll

In result array, index 0 is the whole matched string, index 1 is first captured group, index 2 is the second, etc.
The result array have these properties:

  • index → The beginning position of first occurrence.
  • input → The input string.
  • groups → (JS2018) A object. Key is name of named capture group. Value is corresponding substring, or undefined if there is none.

Normal example. Use exec to capture patterns in a string. Do not add global flag.

// capture the attribute values in a image tag
const txt = `<img src="cat.jpg" alt="my cat">`;
const gx = /<img src="([^"]+)" alt="([^"]+)">/;
const myResult = gx.exec(txt);
console.log(myResult);
// [ '<img src="cat.jpg" alt="my cat">', "cat.jpg", "my cat" ]

Yes capture group, no global flag. Return captured groups.

const gx = /(\d)/;
console.log(gx.lastIndex); // 0
console.log(gx.exec("a2cd3f")); // [ "2", "2" ]
console.log(gx.lastIndex); // 0
console.log(gx.exec("a2cd3f")); // [ "2", "2" ]
console.log(gx.lastIndex); // 0

Yes capture group, yes global flag. Return captured groups. Advance RegExp.prototype.lastIndex.

const gx = /(\d)/g;
console.log(gx.lastIndex); // 0
console.log(gx.exec("a2cd3f")); // [ "2", "2" ]
console.log(gx.lastIndex); // 2
console.log(gx.exec("a2cd3f")); // [ "3", "3" ]
console.log(gx.lastIndex); // 5

No capture group, no global flag. Return 1st match, in a array. (not useful)

const gx = /\d/;
console.log(gx.lastIndex); // 0
console.log(gx.exec("a2cd3f")); // [ "2" ]
console.log(gx.lastIndex); // 0
console.log(gx.exec("a2cd3f")); // [ "2" ]
console.log(gx.lastIndex); // 0

No capture group, yes global flag. Return 1st match. (array of 1 item) Advance RegExp.prototype.lastIndex.

const gx = /\d/g;
console.log(gx.lastIndex); // 0
console.log(gx.exec("a2cd3f")); // [ "2" ]
console.log(gx.lastIndex); // 2
console.log(gx.exec("a2cd3f")); // [ "3" ]
console.log(gx.lastIndex); // 5

Show result object properties index and input.

const txt = "a x1 X2 c";
const gx = /\w+/ig;
const myResult = gx.exec(txt);

console.log(myResult.input); // a x1 X2 c
console.log(myResult.index); // 2
console.log(gx.lastIndex); // 4

Show result object property groups.

// capture alt text in img tag
const txt = `<img src="cat.jpg" alt="my cat">`;

const gx = /<img src="[^"]+" alt="(?<altText>[^"]+)">/;

const myResult = gx.exec(txt);

console.log(myResult);
// [ '<img src="cat.jpg" alt="my cat">', "my cat" ]

// show result object properties
console.log(myResult.groups);
// { altText: "my cat" }

JavaScript, Regular Expression