A regular expression to extract a domain name or subdomain (with a protocol like HTTPS, HTTP) from a given URL.
/^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?: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:
- Domain Name Regular Expression
- Regular Expression To Match All URLs In Text
- Regex To Match One Parameter In URL Query String
- URL (With And Without Port Number) Regular Expressions
- Regex To Extract Filename & Domain Name From URL
- Regex To Extract Filename From A Path
- Regular Expression To Extract File Extension From String