从URL捕获顶级域时发出问题 [英] Issue while capturing Top-Level Domain from URL

查看:85
本文介绍了从URL捕获顶级域时发出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一种从URL捕获顶级域名的方法,但是无法取得任何成功。我的问题是URL可能不同。有时用户可以输入www.google.com或m.google.com或m.google.uk或google.uk或www.m.google.com

I want a way to capture the Top-Level Domain from a URL, but am not able to get any success. The problem in my case is that the URL can be different. Sometimes a user can enter www.google.com or m.google.com or m.google.uk or google.uk or www.m.google.com

I尝试使用 slice ,但它不起作用,因为我的URL中可以有2或3个字符。我不能基于。进行拆分,我可能得到2或3或4个结果。我可以使用单行JavaScript函数吗?有没有可用的简单自定义功能?

I tried using slice but it didn't work as I can have 2 or 3 characters in my URL. I can't split based on ".", I might get 2 or 3 or 4 results. Is there a single-line JavaScript function I can use? Is there any easy custom function available?

所有帖子都指向获取主机名,但在我的情况下,我想提取URL的最后3或2个字符( com,uk,cn等)。我也可以应用多个if-else循环,但我想避免这种情况,并想检查是否有一个简单的解决方案。

All posts are pointing to get the host name but in my case I want to extract just last 3 or 2 characters of the URL (com, uk, cn, etc.). I can apply multiple if-else loops too but I want to avoid that, and want to check if there is a simple solution for this.

我正在寻找输出为'com'或'uk'或'cn'取决于我的网址的顶级域名。用户输入了网址,这就是为什么很难预测用户是否会输入m.google.com或www.m.google.com或www.google.com或只是google.com

I am looking for output as 'com' or 'uk' or 'cn' depending on top level domain of my URL. URL is entered by user which is why it difficult to predict if user will enter m.google.com or www.m.google.com or www.google.com or simply google.com

推荐答案

下面的代码适用于我。感谢@StephenP的帮助。还要感谢@Timo,但似乎Document未在量角器库中识别。

Below code works for me. Thanks @StephenP for your help. Thanks @Timo as well but it seems Document is not identified in protractor library.

var parser = TextBox.siteName;//get input of site from user in parser variable.
 var hostParts = parser.split('.');
    var URLdomain = hostParts[hostParts.length - 1];

这篇关于从URL捕获顶级域时发出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆