Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda