gpt4 book ai didi

javascript - IE中如何从URL字符串中获取域名

转载 作者:行者123 更新时间:2023-12-02 16:17:02 26 4
gpt4 key购买 nike

我有一个 AngularJs 过滤器返回给定 URL 字符串的域名。

app.filter('domain', function() {
return function(input) {
if (input) {
// remove www., add http:// in not existed
input = input.replace(/(www\.)/i, "");
if (!input.match(/(http\:)|(https\:)/i)) {
input = 'http://' + input;
})

var url = new URL(input);
return url.hostname;
}
return '';
};
});

问题是因为我不支持 URL() 方法,所以它在 IE 中不起作用。

最佳答案

是的,根据this document IE 不支持 URL() 接口(interface)。但让我们跳出框框吧!您的过滤器可以用更短、更快速的方式编写:

app.filter('domain', function() {
return function(input) {
if (input) {
input = input.replace(/(www\.)/i, "");
if( !input.replace(/(www\.)/i, "") ) {
input = 'http://' + input;
}

var reg = /:\/\/(.[^/]+)/;
return input.match(reg)[1];
}
return '';
};
});

关于javascript - IE中如何从URL字符串中获取域名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29488495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com