DEV Community

Daniel Zotti
Daniel Zotti

Posted on

#LearnedToday: Named Capture Group

Yesterday I worked on a task for which the regular expressions were perfect.

Today I would like to share a feature that is supported since 2018 but that I have seen used very little: Named Capturing Group.

I really love this feature because it allows for neater and more readable regex.

const regex = /(?<day>\d+)\/(?<month>\d+)\/(?<year>\d+)/;

const { day, month, year } = regex.exec("16/04/1987").groups;

console.log({ day, month, year});
// {day: '16', month: '04', year: '1987'}
Enter fullscreen mode Exit fullscreen mode

I have created a simple example on stackblitz to hopefully make their use understood.

I also created an example on Regex101, a tool I always use to test regular expressions.

đź”— Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Named_capturing_group

â„ą Browser support: https://caniuse.com/mdn-javascript_regular_expressions_named_capturing_group

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more