Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
Javascript return true if a string contains subdomain

I have an API that returns a domain to my front end.

This domain is the string format.

For eg: "google.com" / "google.co.ok"
or "test.google.com"/ "test.google.co.ok:

Notice that the string does not contain any protocol.

I want to write a method that parses the string and returns true if the string contains a subdomain.

In the above 2 examples, the method should return true for test.google.com or test.google.co.ok

EDIT: If it were python, i would write something like below. But hoping something similat was available in 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'
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

There are multiple JavaScript libraries available that can be used the same way you're using tld. psl is older but still has millions of weekly downloads.

You could use psl and implement something like this:

import { parse } from "psl";

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

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

Feel free to clone and edit this example on RunKit as you see fit.

about 4 years ago · Juan Pablo Isaza Relatório

0

Sure thing. Since there's no protocol, maybe something like:

"word.domain.com"
  .split(".").length > 2 // true

"domain.com"
  .split(".").length > 2 // false

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

You'll likely need to parse out "www" and second-level domains (".co", ".gc", etc).

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use RegExp to perform string manipulation. Please take a look at the following snippet and run the code and see the results from different test cases covering most of the possibilities. Let me know if it's helpful.

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)}`);

return(subDomain);

}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda