Regex To Extract Domain Name From URL

Category: URI

A regular expression to extract a domain name or subdomain (with a protocol like HTTPS, HTTP) from a given URL.

/^(?:https?:\/\/)?(?:[^@\/\n][email protected])?(?:www\.)?([^:\/?\n]+)/

Matches:

  • https://regexpattern.com/post.php?post=145&action=edit
  • http://regexpattern.com/post.php?post=145&action=edit
  • https://sub.regexpattern.com/post.php?post=145&action=edit
  • regexpattern.com/post.php?post=145&action=edit

Non-matches:

  • ftp://regexpattern.com/post.php?post=145&action=edit
  • //regexpattern.com/post.php?post=145&action=edit

See Also:

Rate This Regex
Sending
User Review
0 (0 votes)

Expression Flags

Flags Description
i Ignore case sensitive.
g Allows global search.
m Allows multiline search.
Category: URI