【发布时间】:2022-01-08 23:00:45
【问题描述】:
我有一个 API 可以将域返回到我的前端。
这个域是字符串格式。
For eg: "google.com" / "google.co.ok"
or "test.google.com"/ "test.google.co.ok:
请注意,该字符串不包含任何协议。
我想写一个解析字符串的方法,如果字符串包含子域则返回true。
在上述 2 个示例中,test.google.com 或 test.google.co.ok
的方法应返回 true编辑:如果是 python,我会写如下内容。但希望在 JS 中有类似的东西。
from tld import get_tld, get_fld
get_tld("www.google.co.uk", fix_protocol=True)
# 'co.uk'
get_fld("www.google.co.uk", fix_protocol=True)
# 'google.co.uk'
【问题讨论】:
-
由于没有协议,可能类似于
"word.domain.co.uk/something".split("/")[0].split(",").length > 2 -
@Dexygen 查看我的编辑。如果我要使用 Python,我知道如何获得答案
-
然后看看Get the domain name of the subdomain Javascript。这足以回答您的问题吗?
-
@DanielSzabo 的解决方案似乎有效。你能把它添加为答案吗
-
你没有写 Python 方法,你使用了库中的方法
标签: javascript reactjs