mercury233
V2EX  ›  问与答

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

  •  
  •   mercury233 · Dec 15, 2022 · 1603 views
    This topic created in 1269 days ago, the information mentioned may be changed or developed.

    将一级域名理解为 .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 replies    2022-12-15 10:36:44 +08:00
    jucelin
        2
    jucelin  
       Dec 15, 2022
    理论上提取不到的,给几个特殊的:
    www.net.cn
    edu.cn

    当然也客户忽略这些,可以用列举法,如 https://whois.aliyun.com/js/domainparser.js
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   893 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 22:36 · PVG 06:36 · LAX 15:36 · JFK 18:36
    ♥ Do have faith in what you're doing.