DEV Community

HAPPYCODING
HAPPYCODING

Posted on • Updated on • Originally published at Medium

Capturing named regex groups with JavaScript

One of my personal greatest discoveries in coding was the power of named regex groups in JavaScript. While numbered groups can be useful, they often lead to confusing and hard-to-maintain code. Named groups, on the other hand, make our regular expressions more readable and our intentions clearer.

Imagine you are parsing a complex string with multiple pieces of data. Using numbered groups, you end up with a mess of indices. Named groups allow us to refer to each part of our regex match with meaningful names, making our code elegant and easy to understand.

Capturing Named Regex Groups in JavaScript with Jest

Let’s dive into some examples to see how we can capture named regex groups using JavaScript. We’ll use Jest for testing our regex patterns.

First, let’s define a simple regex pattern to capture a date in the format “YYYY-MM-DD”:

const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
Enter fullscreen mode Exit fullscreen mode

In this pattern, year, month, and day are named groups. Now, let's write a Jest test to ensure our regex works correctly:

test('captures named groups for a date', () => {
  const date = '2024-05-19';
  const match = date.match(dateRegex);

  expect(match?.groups?.year).toBe('2024');
  expect(match?.groups?.month).toBe('05');
  expect(match?.groups?.day).toBe('19');
});
Enter fullscreen mode Exit fullscreen mode

Here, we use the groups property of the match object to access the named groups. The ? operator ensures we handle cases where the match is null or undefined gracefully.

Let’s look at another example, capturing parts of an email address:

const emailRegex = /(?<localPart>[a-zA-Z0-9._%+-]+)@(?<domain>[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/;

test('captures named groups for an email', () => {
  const email = 'example.user@example.com';
  const match = email.match(emailRegex);

  expect(match?.groups?.localPart).toBe('example.user');
  expect(match?.groups?.domain).toBe('example.com');
});
Enter fullscreen mode Exit fullscreen mode

In this pattern, localPart and domain are the named groups. The Jest test verifies that our regex correctly captures these parts of the email address.

Conclusion: The Joy of Named Groups

And so, after diving into the world of named regex groups in TypeScript, I’ve come to a delightful realization: named groups are like giving our regex matches a well-deserved promotion. No longer do they toil in the obscurity of numbered indices. Instead, they proudly wear their meaningful names, bringing clarity and joy to our code. So, the next time you find yourself tangled in a web of regex patterns, remember: named groups are your friends, here to make your coding life just a little bit brighter.

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

This is pure JavaScript and has nothing to do with TypeScript

Collapse
 
happycoding profile image
HAPPYCODING

Shame of me. Thanks for pointing this out. :) My mind was on Typescript when writing this article.