Special Characters: Capturing and Non-Capturing Groups

Let's learn about capturing and non-capturing groups with the help of an example.

Introduction

Groups are an incredibly useful part of regular expressions because they allow you to grab a portion of the matching pattern and use it in your code logic or reference it while doing a string replacement.

The groups you can grab and reference later are called capturing groups, which, as the name suggests, means you’re able to capture them and use them however you want. On the other hand, you have non-capturing groups, which allow you to organize your matched expressions without the added benefit of having individual access to them for further operations. Take a look at the example down below to learn how these can be useful.

Capturing the hostname of a URL

Let’s pretend you need to capture the hostname of a URL from a particular string. In fact, let’s use the following URLs as examples:

When trying to create a regular expression, it’s always useful to use several variations of a string to test your results rather than using a single string. This is because you’re creating a pattern that should be able to match (and in this case capture) several variations and not just a single string.

Notice how we have three different URLs with different protocols, different hostnames (including the optional www), and different URIs. That’s the kind of variation you want when creating your list of testing strings.

So, what we want to do is ensure we’re matching something that starts with a protocol section, followed by the hostname and potentially has a URI.

Get hands-on with 1200+ tech skills courses.