Groups and Capturing

Groups and Capturing

Grouping Subpatterns

Grouping in Regex allows you to treat multiple characters as a single unit. Groups are created by placing the characters to be grouped inside parentheses ().

Example: Grouping Subpatterns

javascript
	const regexGroup = /(abc)+/;
	console.log(regexGroup.test('abcabcabc')); // true (matches 'abc' repeated one or more times)
	console.log(regexGroup.test('abcxyz')); // true (matches 'abc' once)
	console.log(regexGroup.test('xyz')); // false (no match)

In this example, (abc)+ matches one or more occurrences of the sequence “abc”.

Capturing Groups

Capturing groups not only group subpatterns but also capture the matched subpattern for later use. Capturing groups are created the same way as regular groups, using parentheses ().

Example: Capturing Groups

javascript
	const regexCapture = /(\d{3})-(\d{2})-(\d{4})/;
	const match = '123-45-6789'.match(regexCapture);
	
	if (match) {
	  console.log(match[0]); // '123-45-6789' (the entire match)
	  console.log(match[1]); // '123' (first capturing group)
	  console.log(match[2]); // '45' (second capturing group)
	  console.log(match[3]); // '6789' (third capturing group)
	}

In this example:

  • (\d{3}) captures the first three digits.
  • (\d{2}) captures the next two digits.
  • (\d{4}) captures the last four digits.

Non-Capturing Groups

Sometimes you need to group subpatterns without capturing them. Non-capturing groups are created by placing ?: at the start of the group.

Example: Non-Capturing Groups

javascript
	const regexNonCapture = /(?:abc)+/;
	const match = 'abcabcabc'.match(regexNonCapture);
	
	console.log(match[0]); // 'abcabcabc' (the entire match)
	console.log(match[1]); // undefined (no capturing group)

In this example, (?:abc)+ matches one or more occurrences of the sequence “abc” without capturing it.

Nested Groups

You can nest groups within other groups to create more complex patterns.

Example: Nested Groups

javascript
	const regexNested = /((\d{3})-(\d{2}))-(\d{4})/;
	const match = '123-45-6789'.match(regexNested);
	
	if (match) {
	  console.log(match[0]); // '123-45-6789' (the entire match)
	  console.log(match[1]); // '123-45' (first capturing group)
	  console.log(match[2]); // '123' (second capturing group)
	  console.log(match[3]); // '45' (third capturing group)
	  console.log(match[4]); // '6789' (fourth capturing group)
	}

In this example:

  • ((\d{3})-(\d{2})) is the first capturing group, which contains two nested capturing groups.
  • (\d{4}) is the fourth capturing group.

Exercises

  1. Write a regular expression that captures the area code, central office code, and line number from a US phone number in the format (123) 456-7890.
  2. Create a Regex pattern that matches and captures the day, month, and year from a date in the format dd/mm/yyyy.
  3. Write a regular expression that uses a non-capturing group to match one or more occurrences of the sequence “cat” in a string without capturing it.

Groups and capturing allow you to work with subpatterns and extract specific parts of the matched strings.