【问题标题】:Javascript return true if a string contains subdomain如果字符串包含子域,Javascript 返回 true
【发布时间】: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


【解决方案1】:

有多个可用的 JavaScript 库,可以像使用 tld 一样使用。 psl 较旧,但每周仍有数百万的下载量。

您可以使用psl 并实现如下内容:

import { parse } from "psl";

function hasSubdomain(str) {
  const { subdomain } = parse(str);
  
  return subdomain !== null;
}

hasSubdomain("www.google.com") // true
hasSubdomain("google.co.uk") // false

随意克隆和编辑此example on RunKit

【讨论】:

    【解决方案2】:

    当然。由于没有协议,可能类似于:

    "word.domain.com"
      .split(".").length > 2 // true
    
    "domain.com"
      .split(".").length > 2 // false
    
    "www.domain.co.uk"
      .split(".").length > 2 // uh-oh
    

    您可能需要解析出“www”和second-level domains(“.co”、“.gc”等)。

    【讨论】:

    • 对于那些“多部分顶级域”(我宁愿称它们为“有效”tlds),请参阅public suffix list
    • hmmm..解析出 www 是有道理的..但是跟踪二级域可能具有挑战性
    【解决方案3】:

    您可以使用RegExp 来执行字符串操作。请查看以下 sn-p 并运行代码,并查看涵盖大多数可能性的不同测试用例的结果。让我知道它是否有帮助。

    function subDomain(url) {
      // REMOVE LEADING AND TRAILING WHITE SPACE 
      url = url.replace(new RegExp(/^\s+/), ""); // START
      url = url.replace(new RegExp(/\s+$/), ""); // END
    
      // CONVERT BACK SLASHES TO FORWARD SLASHES
      url = url.replace(new RegExp(/\\/g), "/");
    
      // REMOVES 'www.' FROM THE START OF THE STRING
      url = url.replace(new RegExp(/^www\./i), "");
    
      // REMOVE STRING FROM FIRST FORWARD SLASH ON
      url = url.replace(new RegExp(/\/(.*)/), "");
    
      // REMOVES '.??.??' OR '.???.??' FROM END - e.g. '.CO.UK', '.COM.AU'
      if (url.match(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i))) {
        url = url.replace(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i), "");
    
        // REMOVES '.??' or '.???' or '.????' FROM END - e.g. '.US', '.COM', '.INFO'
      } else if (url.match(new RegExp(/\.[a-z]{2,4}$/i))) {
        url = url.replace(new RegExp(/\.[a-z]{2,4}$/i), "");
      }
    
      // CHECK TO SEE IF THERE IS A DOT '.' LEFT
      var subDomain = url.match(new RegExp(/\./g)) ? true : false;
    
      return subDomain;
    }
    
    const subdomainInput = "test.google.com";
    const subdomainInputWithPath = "test.google.com/test";
    const subdomainInputWithPathWithWS = "    test.google.com    ";
    const subdomainInputWithWS = "   test.google.com    ";
    const subdomainInputWithQueryString = "test.google.com/test?token=33333";
    const noSubInput = "google.com"
    const noSubInputWithPath = "google.com/search"
    const noSubInputWithPathWithQueryString = "google.com/search?token=ttttttt"
    console.log("Test Run\n")
    conosle.log("With subdomain test cases")
    console.log(`subdomainInput: ${subDomain(subdomainInput)}`);
    console.log(`subdomainInputWithPath: ${subDomain(subdomainInputWithPath)}`);
    console.log(`subdomainInputWithWS: ${subDomain(subdomainInputWithWS)}`);
    console.log(`subdomainInputWithPathWithWS: ${subDomain(subdomainInputWithPathWithWS)}`);
    console.log(`subdomainInputWithQueryString: ${subDomain(subdomainInputWithQueryString)}`);
    
    conosle.log("Without subdomain test cases")
    console.log(`noSubInput: ${subDomain(noSubInput)}`);
    console.log(`noSubInput: ${subDomain(noSubInput)}`);
    console.log(`noSubInputWithPath: ${subDomain(noSubInputWithPath)}`);
    console.log(`noSubInputWithPathWithQueryString: ${subDomain(noSubInputWithPathWithQueryString)}`);

    返回(子域);

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      相关资源
      最近更新 更多