This is the sixth day of my participation in the August Text Challenge.More challenges in August

The exec() method performs a search match in a specified string. Returns a result array or NULL.

JavaScript RegExp objects are stateful when the global or sticky flag bits are set (e.g. /foo/g or /foo/y). They record the position since the last successful match in the lastIndex property. With this feature, exec() can be used to iterate over multiple matches in a single String (including captured matches), whereas string.prototype.match () only returns matched results.

If you’re just trying to determine whether a match is true or false, you can use the regexp.test () method, or the string.search () method.

JavaScript Demo: RegExp.prototype.exec()

const regex1 = RegExp('foo*'.'g');
const str1 = 'table football, foosball';
let array1;

while((array1 = regex1.exec(str1)) ! = =null) {
  console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}. `);
  // expected output: "Found foo. Next starts at 9."
  // expected output: "Found foo. Next starts at 19."
}

Copy the code

grammar

regexObj.exec(str)
Copy the code

parameter

STR The string to match the regular expression.

The return value

If the match is successful, the exec() method returns an array containing the additional attributes index and input, as shown in the table below, and updates the lastIndex attribute of the regular expression object. The fully matched text is returned as the first item in the array, and from the second, each subsequent item corresponds to the successfully matched text in the regular expression capture brackets.

If the match fails, the exec() method returns null and resets lastIndex to 0.

describe

Consider the following example:

// Match "quick brown" followed by "jumps", ignoring characters in between
// Remember "brown" and "jumps"
// Ignore case
var re = /quick\s(brown).+? (jumps)/ig;
var result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog');
Copy the code

The following table lists the return values of this script:

The sample

Find all matches

When a regular expression uses the “G” flag, the exec method can be executed multiple times to find a successful match in the same string. When you do this, the search will start at the location specified by the lastIndex property of the regular expression. Test () also updates the lastIndex property. Note that lastIndex is not reset even if the string searched again is not the original searched string, and it still starts from the recorded lastIndex.

For example, you use the following script:

var myRe = /ab*/g;
var str = 'abbcdefabh';
var myArray;
while((myArray = myRe.exec(str)) ! = =null) {
  var msg = 'Found ' + myArray[0] + '. ';
  msg += 'Next match starts at ' + myRe.lastIndex;
  console.log(msg);
}
Copy the code

The command output is as follows:

Found abb. Next match starts at 3
Found ab. Next match starts at 9
Copy the code

! Note: Do not put regular expression literals (or RegExp constructors) in while conditional expressions. Since the lastIndex property is reset with each iteration, a match would create an infinite loop. And make sure the ‘G’ flag is used for global matching, otherwise it will also cause an endless loop.

Exec () with the RegExp literal

You can also use exec() directly instead of creating a RegExp object:

var matches = /(hello \S+)/.exec('This is a hello world! ');
console.log(matches[1]);
Copy the code

Run the above code and the console will print “Hello world!” A string.

conclusion