Can someone help me understand the exec method for regular expressions?

The best place I've found for the exec method is Eloquent Javascript Chapter 9:

"Regular expressions also have an exec (execute) method that returns null if no match is found, and return an object with match information otherwise. The object returned by exec has an index property that tells us where the successful match starts in the string . In addition, the object looks like (but actually has) an array of strings whose first element is the string that was matched ... "

This still makes sense, but then it gets a little confused:

"When a regular expression contains subexpressions grouped with parentheses, the text corresponding to these groups will also be displayed in the array. All matches are always the first element."

okay, but...

"The next element is the part matched by the first group (the first in which the opening bracket is included in the expression), then the second group, etc.

var quotedText = /'([^']*)'/;
console.log(quotedText.exec("she said 'hello'"));
// → ["'hello'", "hello"]

My confusion is the re-greeting in this example. I don’t understand why this will return two assistants to me?

And then the topic ends with the following:

"When a group does not end at all (for example, when it is followed by a question mark), its position in the output array will be undefined. Similarly, when a group is matched several times, only the last match ends in the array."

console.log(/bad(ly)?/.exec("bad"));
// → ["bad", undefined]
console.log(/(\d)+/.exec("123"));
// → ["123", "3"]

This last sentence and example confuse me ....

!

+4
1

, ?

- , , . , . - 'hello' ( ), - hello ( ), ( ) hello, ' :

 vvvvvvvvv----- Overall expression
/'([^']*)'/
  ^^^^^^^------ Capture group

/bad(ly)?/: , , "match bad, ly, ly, ". , :

console.log(/bad(ly)?/.exec("bad"));
// -> ["bad", undefined]
//     ^      ^
//     |      +--- first capture group has nothing in it
//     +---------- overall match is "bad"
console.log(/bad(ly)?/.exec("badly"));
// -> ["badly", "ly"]
//     ^        ^
//     |        +- first capture group has "ly"
//     +---------- overall match is "badly"

, l y :

console.log(/bad(l)?(y)?/.exec("bad"));
// -> ["bad", undefined, undefined]
//     ^      ^          ^
//     |      |          +--- Nothing in the second capture group
//     |      +-------------- Nothing in the first capture group
//     +--------------------- Overall match is "bad"
console.log(/bad(l)?(y)?/.exec("badly"));
// -> ["badly", "l", "y"]
//     ^        ^    ^
//     |        |    +------- Second capture group has "y"
//     |        +------------ First capture group has "l"
//     +--------------------- Overall match is "badly"
console.log(/bad(l)?(y)?/.exec("badl"));
// -> ["badl", "l", undefined]
//     ^       ^    ^
//     |       |    +-------- Second capture group has nothing in it
//     |       +------------- First capture group has "l"
//     +--------------------- Overall match is "badl"
console.log(/bad(l)?(y)?/.exec("bady"));
// -> ["bady", undefined, "y"]
//     ^       ^          ^
//     |       |          +-- Second capture group has "y"
//     |       +------------- First capture group has nothing in it
//     +--------------------- Overall match is "bady"
+7

All Articles