正则表达式需要删除域名

前端之家收集整理的这篇文章主要介绍了正则表达式需要删除域名前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要一个正则表达式来删除网址的域名部分.例如,如果我有以下网址:

http://www.website-2000.com

我希望正则表达式匹配的位是’website-2000′

如果您还可以解释正则表达式的每个部分如何帮助我理解它会很棒.

谢谢

这应该工作.它可能有一些缺点,但我现在无法想到.如果有人想改进它,请随意这样做.
/http:\/\/(?:www\.)?([a-z0-9\-]+)(?:\.[a-z\.]+[\/]?).*/i

http:\/\/            matches the "http://" part
(?:www\.)?           is a non-capturing group that matches zero or one "www."
([a-z0-9\-]+)        is a capturing group that matches character ranges a-z,0-9
                     in addition to the hyphen. This is what you wanted to extract.
(?:\.[a-z\.]+[\/]?)  is a non-capturing group that matches the TLD part (i.e. ".com",".co.uk",etc) in addition to zero or one "/"
.*                   matches the rest of the url

http://rubular.com/r/ROz13NSWBQ

原文链接:https://www.f2er.com/regex/357233.html

猜你在找的正则表达式相关文章