JavaScript RegExp Groups

Back to JS page

Table of Contents

RegExp Groups

Groups allow you to combine multiple patterns as a single unit:

Syntax Description
(x) Capturing group - remembers the match
(?<name>x) Named capturing group
(?:x) Non-capturing group
x(?=y) Lookahead
x(?!y) Negative lookahead
(?<=y)x Lookbehind
(?<!y)x) Negative lookbehind

RegExp Capturing Groups (x)

Capturing groups (...) group patterns and remember the match:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript RegExp Groups</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>RegExp Capturing Groups (x)</h4>
<p id="demo"></p>

<script>
const text = "John Smith, Jane Doe";
const matches = text.match(/(\w+) (\w+)/);

document.getElementById("demo").innerHTML =
  "Text: " + text + "<br><br>" +
  "match(/(\\w+) (\\w+)/):<br>" +
  "Full match: " + matches[0] + "<br>" +
  "Group 1: " + matches[1] + "<br>" +
  "Group 2: " + matches[2];
</script>

</body>
</html>

Example 1

Result View Example

The Result Array

When using match(), capturing groups appear in the result array:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript RegExp Groups</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The Result Array</h4>
<p id="demo"></p>

<script>
const text = "Date: 2024-05-15";
const matches = text.match(/(\d{4})-(\d{2})-(\d{2})/);

let text2 = "Text: " + text + "<br><br>";
text2 += "match(/(\\d{4})-(\\d{2})-(\\d{2})/):<br><br>";
text2 += "matches[0] (full): " + matches[0] + "<br>";
text2 += "matches[1] (year): " + matches[1] + "<br>";
text2 += "matches[2] (month): " + matches[2] + "<br>";
text2 += "matches[3] (day): " + matches[3] + "<br><br>";
text2 += "index: " + matches.index + "<br>";
text2 += "input: " + matches.input;

document.getElementById("demo").innerHTML = text2;
</script>

</body>
</html>

Example 2

Result View Example

Named Capturing Groups (?)

Named groups use (?<name>...) syntax. You can access matches by name:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript RegExp Groups</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Named Capturing Groups (?<n>)</h4>
<p id="demo"></p>

<script>
const text = "Date: 2024-05-15";
const matches = text.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);

document.getElementById("demo").innerHTML =
  "Text: " + text + "<br><br>" +
  "Named groups:<br>" +
  "matches.groups.year: " + matches.groups.year + "<br>" +
  "matches.groups.month: " + matches.groups.month + "<br>" +
  "matches.groups.day: " + matches.groups.day;
</script>

</body>
</html>

Example 3

Result View Example

Non-Capturing Groups (?:...)

Non-capturing groups (?:...) group patterns without remembering the match:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript RegExp Groups</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Non-Capturing Groups (?:...)</h4>
<p id="demo"></p>

<script>
const text = "apple orange apple orange";
const matches = text.match(/(?:apple|orange)/g);

document.getElementById("demo").innerHTML =
  "Text: " + text + "<br><br>" +
  "match(/(?:apple|orange)/g): " + matches + "<br><br>" +
  "Non-capturing groups don't save to result array:<br>" +
  "matches.length: " + matches.length;
</script>

</body>
</html>

Example 4

Result View Example

Document

Document in project

You can Download PDF file.

Reference