In JavaScript regular expressions (RegEx), a group is a way to create a subpattern within a larger pattern. Groups are denoted by enclosing the desired subpattern with parentheses (
and )
.
Here are some commonly used syntaxes for creating groups:
1. Capturing Group: To create a capturing group, enclose the desired subpattern within parentheses.
const pattern = /(abc)/;
2. Non-Capturing Group: To create a non-capturing group, use (?:...)
. This allows you to group a subpattern without capturing it as a separate group in the result array.
const pattern = /(?:abc)/;
3. Named Capturing Group: You can assign a name to a capturing group by using (?<name>...)
syntax. This allows you to access the captured value by its name in addition to the numeric index.
const pattern = /(?<word>\w+)/;
4. Positive Lookahead Group: A positive lookahead group is denoted by (?=...)
. It matches a subpattern only if it is followed by another pattern.
const pattern = /abc(?=def)/;
5. Negative Lookahead Group: A negative lookahead group is denoted by (?!...)
. It matches a subpattern only if it is not followed by another pattern.
const pattern = /abc(?!def)/;
Groups are useful for capturing specific parts of a string or applying quantifiers to subpatterns.
JavaScript RegEx group example
Simple example code.
const text = 'The quick brown fox jumps over the lazy dog.';
// Pattern with two groups to capture the words "quick" and "fox"
const pattern = /The (quick) brown (fox) jumps over the lazy dog./;
// Executing the pattern with the test string
const result = pattern.exec(text);
// Accessing the captured groups
const adjective = result[1];
const animal = result[2];
console.log(adjective);
console.log(animal);
Output:
How do you access the matched groups in a JavaScript regular expression?
Answer: In JavaScript, when you execute a regular expression pattern using methods like exec()
or match()
, the matched groups can be accessed using the resulting match object or array. Here’s how you can access the matched groups:
The exec()
method returns an array where the first element (result[0]
) is the entire matched string, and subsequent elements correspond to the captured groups. To access the captured groups, you can use numeric indices or named capture groups (if specified).
const text = 'Hello, my name is John Doe.';
const pattern = /Hello, my name is ([A-Za-z\s]+)/;
const result = pattern.exec(text);
const name = result[1]; // Access the captured group using index 1
console.log(name); // Output: John Doe
The match()
method returns an array of all matches, including captured groups. If the regular expression has the global flag (/g
), it returns an array of multiple matches. Each element in the resulting array corresponds to a match, and the captured groups can be accessed using numeric indices.
const text = 'Hello, my name is John Doe.';
const pattern = /Hello, my name is ([A-Za-z\s]+)/g;
const result = text.match(pattern);
const name = result[0][1]; // Access the captured group using index 1
console.log(name); // Output: John Doe
Comment if you have any doubts or suggestions on this Js RegEx topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version