Skip to content

Instantly share code, notes, and snippets.

@klein-artur
Last active January 10, 2022 18:56
Show Gist options
  • Save klein-artur/dad30b98222ac162b178a98fdb64d729 to your computer and use it in GitHub Desktop.
Save klein-artur/dad30b98222ac162b178a98fdb64d729 to your computer and use it in GitHub Desktop.
Regex to split an url into its parts:

Regex to split an url into its parts:

This regex splits an http or https url into its parts.

^(?:(?:(?:(http[s]?):\/\/)(?:([a-zA-Z0-9]+)?\.)?([^:\/\s\?]+\.+[^:\/\s\?]+))?(?:((?:\/[^\/\s]+)*\/)?(?:([\w\-\.]+[^#?\s]+))?)?(#[^#?\s]+)?(?:\?+([^\.\s$]+)?)?)$

Groups

  1. Whole match
  2. Protocol
  3. Subdomain
  4. Domain
  5. Path
  6. File
  7. Anchor id (That #scrollto things)
  8. Parameters

Example:

http://sub.domain.de/here/is/the/file.php#home?test=1 will turn to:

  1. http
  2. sub
  3. domain.de
  4. /here/is/the/
  5. file.php
  6. #home
  7. test=1

not existent parts will lead to empty groups. Have fun using this :-)

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