Last active
March 18, 2017 03:45
-
-
Save sacarino/293774127cde6eb2fd3748a59fc9c1a5 to your computer and use it in GitHub Desktop.
Incredibly useful regex for URL parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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