exec() in JavaScript – How to Execute a RegExp Search
Whenever you use exec()
on a regular expression, the method does the following:
- It executes a search for the regular expression’s pattern in its string argument.
- If the pattern gets found,
exec()
returns an array containing the matched text. Otherwise, the method returnsnull
.
Use RegExp’s exec() method to search a string for a regular expression pattern.
Syntax of the exec()
Method
exec()
accepts a string argument. Here is the syntax:
RegExp.exec("string");
Examples
Below are examples of the exec()
method.
Execute a search for a case-insensitive Color
pattern
/Color/i.exec("My best color is blue.");
// The invocation above will return:["color"];
The snippet above returned ["color"]
because the computer found the RegExp pattern in the string argument.
In other words, the executed search for a case-insensitive Color
pattern in the string argument matched the string’s color
text. Therefore, exec()
returned an array containing the matched text.
Execute a search for Sweetly
at the end of "CodeSweetly"
/Sweetly$/.exec("CodeSweetly");
// The invocation above will return:["Sweetly"];
Execute a search for @
in a string
/@/.exec("Learn JavaScript at codesweetly.com");
// The invocation above will return: null
How Does exec()
Work with Capturing Groups?
Suppose exec()
’s regular expression contains a capturing group. In that case, the method will return an array containing the matched text and each capturing group’s matches.
Below are some examples.
Execute a Search for a Pattern Containing One Capturing Group
/d(a)y/.exec("Tuesday");
// The invocation above will return:['day', 'a', index: 4, input: 'Tuesday', groups: undefined]
In the returned array above,
"day"
refers to the matched text."a"
indicates the matched capturing group pattern.index
refers to the index position where the matched pattern began.input
indicatesexec()
’s string argument.groups
contain a collection of the named capturing groups specified in the regular expression. Orundefined
if the RegExp does not have any named capturing group.
Let’s now see another example.
Execute a Search for a Pattern Containing Two Capturing Groups
/(su)nd(a)y/i.exec("It's a sunny Sunday!");
// The invocation above will return:['Sunday', 'Su', 'a', index: 13, input: "It's a sunny Sunday!", groups: undefined]
In the returned array above,
"Sunday"
refers to the matched text."Su"
indicates the first matched capturing group pattern."a"
indicates the second matched capturing group pattern.index
refers to the index position where the matched pattern began.input
indicatesexec()
’s string argument.groups
contain a collection of the named capturing groups specified in the regular expression. Orundefined
if the RegExp does not have any named capturing group.
Now, let’s see an example with named capturing groups.
Execute a Search for a Pattern Containing Two Named Capturing Groups
/(?<groupOf2>su)nd(?<bestLetter>a)y/i.exec("It's a sunny Sunday!");
// The invocation above will return:['Sunday', 'Su', 'a', index: 13, input: "It's a sunny Sunday!", groups: {groupOf2: 'Su', bestLetter: 'a'}]
In the returned array above,
"Sunday"
refers to the matched text."Su"
indicates the first matched capturing group pattern."a"
indicates the second matched capturing group pattern.index
refers to the index position where the matched pattern began.input
indicatesexec()
’s string argument.groups
contain a collection of the named capturing groups specified in the regular expression.
Suppose you need to include each capturing group’s start and end indices in the returned array. In that case, add a d
flag to your regular expression. Below is an example.
Execute a Search for a Pattern and Output the Capturing Groups’ Indices
const string = "George grew 77 times greater than we imagined 1777077777 years ago.";
const result = /(77) (times)/d.exec(string);
result.indices;
// The invocation above will return:[ [12, 20], [12, 14], [15, 20],];
In the snippet above, note the following:
[12, 20]
contains the start and end indices of the two capturing groups—that is,(77)
and(times)
.[12, 14]
contains the start and end indices of the first capturing group—that is,(77)
.[15, 20]
contains the second capturing group’s start and end indices—that is,(times)
.