Skip to content

Instantly share code, notes, and snippets.

@therightstuff
Last active May 12, 2022 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therightstuff/bc2ff4dcf73a9809b01e056d85828bb3 to your computer and use it in GitHub Desktop.
Save therightstuff/bc2ff4dcf73a9809b01e056d85828bb3 to your computer and use it in GitHub Desktop.
Reading and writing regular expressions for sane people
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})(0–9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
...
// parse a url, only capture the host part
// eg. protocol://host
// protocol://host:port/path?querystring#anchor
// host
// host/path
// host:port/path
// protocol://host:port/path?querystring#anchor
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})(0–9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
...
// parse a url, only capture the host part
// (?:([A-Za-z]+):)?
// protocol — an optional alphabetic protocol followed by a colon
// (\/{0,3})(0–9.\-A-Za-z]+)
// host — 0–3 forward slashes followed by alphanumeric characters
// (?::(\d+))?
// port — an optional colon and a sequence of digits
// (?:\/([^?#]*))?
// path — an optional forward slash followed by any number of
// characters not including ? or #
// (?:\?([^#]*))?
// query string — an optional ? followed by any number of
// characters not including #
// (?:#(.*))?
// anchor — an optional # followed by any number of characters
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})(0–9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment