Skip to content

Instantly share code, notes, and snippets.

@bsara
Last active February 17, 2023 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bsara/8313247 to your computer and use it in GitHub Desktop.
Save bsara/8313247 to your computer and use it in GitHub Desktop.
Regex: URLs
(f|ht)tp(s)?://([A-Za-z0-9-]+\.)+[A-Za-z0-9-]+(/[A-Za-z0-9- ./?%&=]*)?
@bsara
Copy link
Author

bsara commented Feb 17, 2023

This description was generated by ChatGPT on 02/17/2023

This regular expression is used to match URLs that start with either "http", "https", "ftp", or "ftps". Here's a breakdown of the individual parts of the expression:

  • (f|ht)tp: This part matches either "ftp" or "http" at the beginning of the URL. The parentheses indicate a group, and the vertical bar "|" indicates an "or" condition.
  • (s)?: This part matches the letter "s" optionally, indicating that the URL may start with either "http" or "https", or "ftp" or "ftps".
  • ://: This part matches the colon and double slash that follow the protocol.
  • ([A-Za-z0-9-]+.)+: This part matches the domain name, which consists of one or more groups of letters, numbers, and hyphens, followed by a period. The plus sign "+" indicates that the preceding group must occur one or more times.
  • [A-Za-z0-9-]+: This part matches the top-level domain, which consists of letters, numbers, and hyphens. It occurs only once.
  • (/[A-Za-z0-9- ./?%&=])?: This part matches the path and query string. The question mark "?" indicates an optional character, and the asterisk "" indicates that the preceding group can occur zero or more times. The path can contain letters, numbers, hyphens, spaces, periods, forward slashes, question marks, percent signs, and ampersands.

So, this regular expression can match URLs such as "http://www.example.com/page", "https://www.example.com/page?id=123", "ftp://example.com", "ftps://example.com/page%20with%20spaces".

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