Javascript RegExp Instance Methods exec()

Introduction

The primary method of a RegExp object is exec().

It is used for capturing groups.

This method accepts a single argument, which is the string on which to apply the pattern.

It returns an array of information about the first match or null if no match was found.

The returned array contains two additional properties:

  • index - the location in the string where the pattern was matched
  • input - the string that the expression was run against.

In the array, the first item is the string that matches the entire pattern.

Any additional items represent captured groups inside the expression.

If there are no capturing groups in the pattern, then the array has only one item.

Consider the following:

let text = "this is a test test test";
let pattern = /this( is a (test)?)?/gi;

let matches = pattern.exec(text);
console.log(matches.index); // 0 
console.log(matches.input); // 
console.log(matches[0]); //
console.log(matches[1]); //

The exec() method returns information about one match at a time even if the pattern is global.

When the global flag is not specified, calling exec() on the same string multiple times will always return information about the first match.

let text = "cat, bat, sat, fat";
let pattern = /.at/;

let matches = pattern.exec(text);
console.log(matches.index); // 0 
console.log(matches[0]); // cat 
console.log(pattern.lastIndex); // 0 

matches = pattern.exec(text);/*  w  ww.j  a  v  a  2s . c om*/
console.log(matches.index); // 0 
console.log(matches[0]); // cat 
console.log(pattern.lastIndex); // 0  

The pattern in this example is not global, so each call to exec() returns the first match only.

lastIndex remains unchanged in non global mode.

With the global g flag set on the pattern, each call to exec() moves further into the string looking for matches:

let text = "cat, bat, sat, fat";
let pattern = /.at/g;
let matches = pattern.exec(text);
console.log(matches.index); // 0 
console.log(matches[0]); // cat 
console.log(pattern.lastIndex); // 3 

matches = pattern.exec(text);/*from   w  w w.  j a  v a  2  s  . c o m*/
console.log(matches.index); // 5 
console.log(matches[0]); // bat 
console.log(pattern.lastIndex); // 8 

matches = pattern.exec(text);
console.log(matches.index); // 10 
console.log(matches[0]); // sat 
console.log(pattern.lastIndex); // 13 

The above pattern is global, so each call to exec() returns the next match in the string until the end of the string is reached.

In global matching mode, lastIndex is incremented after each call to exec().

lastIndex tracks the index of the character that appears immediately to the right of the last match.

If sticky y flag is set on the pattern, each call to exec() will search for a match in the string only at lastIndex.

The sticky y flag overrides the global flag.

let text = "cat, mat, sat, fat";
let pattern = /.at/y;

let matches = pattern.exec(text);
console.log(matches.index); // 0 
console.log(matches[0]); // cat 
console.log(pattern.lastIndex); // 3 

// There is no match starting at character index 3, 
// exec() will return null
// exec() finds no matches resets lastIndex to 0 
matches = pattern.exec(text);/*from w w w.j a  v a 2s.c  om*/
console.log(matches); // null 
console.log(pattern.lastIndex); // 0 

// Advancing lastIndex will allow a sticky regex exec() to find the next match: 
pattern.lastIndex = 5;
matches = pattern.exec(text);
console.log(matches.index); // 5 
console.log(matches[0]);    
console.log(pattern.lastIndex); // 8 



PreviousNext

Related