V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
mercury233
V2EX  ›  问与答

JavaScript 中如何在考虑 .com.cn 的情况下获取一个 URL 的二级域名?

  •  
  •   mercury233 · 2022-12-15 10:08:43 +08:00 · 733 次点击
    这是一个创建于 469 天前的主题,其中的信息可能已经有所发展或是发生改变。

    将一级域名理解为 .com 或者 .com.cn 或者 .co.uk 等等

    let addr1 = new URL("https://www.example.com/blahblah");
    let addr2 = new URL("https://example.com/blahblah");
    let addr3 = new URL("https://test.example.com/blahblah");
    let addr4 = new URL("https://another.test.example.com/blahblah");
    

    都需要取到二级域名 example.com

    let addr1 = new URL("https://www.example.com.cn/blahblah");
    let addr2 = new URL("https://example.com.cn/blahblah");
    let addr3 = new URL("https://test.example.com.cn/blahblah");
    let addr4 = new URL("https://another.test.example.com.cn/blahblah");
    

    都需要取到二级域名 example.com.cn

    2 条回复    2022-12-15 10:36:44 +08:00
    yuzo555
        1
    yuzo555  
       2022-12-15 10:18:22 +08:00   ❤️ 1
    jucelin
        2
    jucelin  
       2022-12-15 10:36:44 +08:00
    理论上提取不到的,给几个特殊的:
    www.net.cn
    edu.cn

    当然也客户忽略这些,可以用列举法,如 https://whois.aliyun.com/js/domainparser.js
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5479 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 08:21 · PVG 16:21 · LAX 01:21 · JFK 04:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.