js匹配域名之间的正则如何写?
js匹配域名之间的正则如何写?
比如下面的域名:
http://www
.
baidu
.com/
https://
360
.com/
taobao
.com
xxx
.com.cn
xxxx
.baidu.com.cn
获取加粗的字符串,其他的http:// https:// com cn com.cn等等域名后缀全部剔除。
mixsoul
9 years, 8 months ago
Answers
sample code
javascript
([ "http://www.baidu.com/", "https://360.com/", "taobao.com", "xxx.com.cn", "xxxx.baidu.com.cn" ]).forEach(function(s) { var m = s.match(/(?:http(?:s)?:\/\/)?(?:www\.)?(.*?)\./); console.log(m ? m[1] : "<no match>"); });
output
baidu
360
taobao
xxx
xxxx
免贵姓喵名霸天
answered 9 years, 8 months ago