Skip to content

Instantly share code, notes, and snippets.

@suplo
Created June 7, 2017 10:43
Show Gist options
  • Save suplo/15bf780bb8aa1a4e487c794672ec68a5 to your computer and use it in GitHub Desktop.
Save suplo/15bf780bb8aa1a4e487c794672ec68a5 to your computer and use it in GitHub Desktop.
Url regex

A single regex to parse and breakup a full URL including query parameters and anchors e.g.

https://www.google.com/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash

^((http[s]?|ftp):/)?/?([^:\/\s]+)((/\w+)/)([\w-.]+[^#?\s]+)(.)?(#[\w-]+)?$

RexEx positions:

url: RegExp['$&'],

protocol:RegExp.$2,

host:RegExp.$3,

path:RegExp.$4,

file:RegExp.$6,

query:RegExp.$7,

hash:RegExp.$8

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