regex needed to strip out domain name

后端 未结 5 1043
终归单人心
终归单人心 2020-12-20 01:09

I need a regexp to strip out just the domain name part of a url. So for example if i had the following url:

http://www.website-2000.com

the bit I\'d want the

5条回答
  •  盖世英雄少女心
    2020-12-20 02:02

    This one allows you not to have to worry about any of the http/https/ftp etc... in front and also captures all your subdomains too.

    (?:www\.)?([a-z0-9\-.]+)(?:\.[a-z\.]+[\/]?).*/i
    

    The only times it fails that I've found are: - If a . precedes the domain/subdomain without any text before it, the . is included in the regex capture. - Emails with . in them will not work. (fix this by checking passed domain first for the @ symbol before running through regex) - Whitespace in the middle of the domain/subdomain

提交回复
热议问题