...

/

Special Characters: Matching the URL and Non-Capturing Groups

Special Characters: Matching the URL and Non-Capturing Groups

Let's continue our example of capturing the hostname of a URL. Additionally, you will also be familiarized with non-capturing groups at the end of this lesson.

Regular expression to match sections of the URL

Finally, we’re looking for anything that comes after the first slash right after your hostname (i.e., after the .com). Again, we’re keeping it simple for now, so matching that should be quite simple. For the breakdown:

/\.com(\/[a-z\.]+)?/g

And for the breakdown:

  • The mandatory .com at the start is clearly escaping the dot at the beginning.
  • The optional condition for the URL is given by
...
Access this course and 1400+ top-rated courses and projects.