Skip to content

Instantly share code, notes, and snippets.

@sacarino
Last active March 18, 2017 03:45
Show Gist options
  • Save sacarino/293774127cde6eb2fd3748a59fc9c1a5 to your computer and use it in GitHub Desktop.
Save sacarino/293774127cde6eb2fd3748a59fc9c1a5 to your computer and use it in GitHub Desktop.
Incredibly useful regex for URL parsing
(http[s]?:\/\/)?([^\/\s]+)\/?([^\/\s]+)\/(.*)\?(.*)\#(.*)\b
example here: https://regex101.com/r/QJdP9m/1
Full match: https://www.somehost.com/path/that/you/can/target?query=butwhy#because
Group 1: https://
Group 2: www.somehost.com
Group 3: path
Group 4: that/you/can/target
Group 5: query=butwhy
Group 6: because
Groups 3 and 4 could be combined, obviously, but I had a case where I needed to rewrite and mask the first directory/subfolder.
The regex to return the entire "path/that/you/can/target" is:
(http[s]?:\/\/)?([^\/\s]+)\/?(.*)\?(.*)\#(.*)\b
example here: https://regex101.com/r/LCK0W3/1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment