Regular Expressions 101

Community Patterns

URLs

1

Regular Expression
ECMAScript (JavaScript)

/
(?<href>(?<origin>(?<protocol>(?<scheme>[a-z][\w\-]{2,}):)?(?:\/\/)?)?(?:(?<username>[^:\s]*):(?<password>[^@\s]*)@)?(?<host>(?<hostname>\B\.{1,2}\B(?=[a-z\d]|\/)|(?:[a-z][a-z-]*)(?:\.[a-z][a-z-\.]*|(?=\/))|(?:[^\s\$]\d[a-z\d-]*)(?:\.[a-z][a-z-\.]*)|(?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)(?:\.(?!$)|$)){4}|localhost)(?:\:(?<port>\d+))?)(?<pathname>\/[^?#\s]*)?(?<search>\?[^#\s]*)?(?<hash>#[^\s]*)?)
/
i

Description

A simple URL regex that can handle most "properly" formed URLs, and some URIs. It features named groups to easily destruct matches. URIs can use relative paths (../ and ./) to resources.

Example (JavaScript)

let url = "https://username:password@example.org:8080/path/to/resource?query=term&query=terms#fragment";
let regex = /{{ URL RegEx }}/i;

let { href, origin, protocol, scheme, username, password, host, hostname, port, pathname, search, hash } = regex.exec(url).groups;

// href → "https://username:password@example.org:8080/path/to/resource?query=term&query=terms#fragment"
// origin → "https://"
// protocol → "https:"
// scheme → "https"
// username → "username"
// password → "password"
// host → "example.org:8080"
// hostname → "example.org"
// port → "8080"
// pathname → "/path/to/resource"
// search → "?query=term&query=terms"
// hash → "#fragment"
Submitted by ephellon - 5 months ago (Last modified 3 months ago)