Skip to content

Instantly share code, notes, and snippets.

@SavannahF
Last active November 12, 2022 13:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SavannahF/61b12a2c880183e1612488f8ec8b4aa3 to your computer and use it in GitHub Desktop.
Save SavannahF/61b12a2c880183e1612488f8ec8b4aa3 to your computer and use it in GitHub Desktop.
Tutorial for Regex Matching a URL

Tutorial for a Regex Matching a URL

A regular expression, or regex, is a sequence of characters that defines a search pattern that can be used to validate user input. This tutorial breaks down a regex used for matching a URL.

Summary

The following regex can be used to validate a URL and has been broken down in this tutorial by its components (anchor, quantifies, grouping constructs, bracket expressions, character classes, and character escapes). Each section describes the symbol, a description, and where the code is found within the regex.

Regex Matching a URL:

`/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/`

Table of Contents

Regex Components

Anchors

Symbol Description Code in Regex
^ Asserts the position at the start of the string. ``/^(h`
$ Asserts the position at the end of the string (or before the end of the string's line terminator, if present). `/?$/``

Quantifiers

Symbol Description Code in Regex
? Matches the previous token between 0 and 1 times, as many times as possible, giving back as needed. (https?:\/\/)? matches the characters https : // literally
\/?$/ matches the character / literally
(*) Matches the prevoius token between 0 and unlimited times, as many times as possible, giving back as needed. ([\/\w \.-]*)*
(+) Matches the previous token between 1 and unlimited times, as many times as possible, giving back as needed. ([\da-z\.-]+)
{2,6} Matches the previous token between 2 and 6 times, as many times as possible, giving back as needed. ([a-z\.]{2,6})

Grouping Constructs

Symbol Description Code in Regex
(?:...) Matches everything enclosed. (https?:\/\/)?

Bracket Expressions

Symbol Description Code in Regex
[a-z] Matches a single character in the range between a and z. [a-z\.] matches a single character in the range between a & z with a character.
[\da-z\.-] matches a single character in the range between a & z, before one digit equivalent to [0-9] & the characters . & -
[ \/ \w \.-] Matching a single character present in the list to the right: \/ = matches the character /
\w = matches any word character equivalent to [a-zA-Z0-9_]
\. = matches the character .
- = matches the character -

Character Classes

Symbol Description Code in Regex
\d Matches a single character that is a digit. ([\da-z\.-]+)
\w Matches any word character. Alphanumeric charatcer plus underscore. [\/\w \.-]

Character Escapes

Symbol Description Code in Regex
\d Matches a single character that is a digit. ([\da-z\.-]+)
\w Matches any word character. Alphanumeric charatcer plus underscore. [\/\w \.-]

Author

Savannah Fortson, student in web development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment