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

142
Vistas
Javascript return only the subdomain part of a string url

I have a string that is typically a url (minus the http/www part). Keep in mind this string is returned via an API and not parsed from the browser url.

For eg "xyz.abc.com or "login.xyz.abc.com"

Now I need to parse the string and return only the subdomain so that I get the following:

"xyz" and "login.xyz"

With Python, I can achieve this using the following code:

".".join(String.split(".")[:-2])

I am unable to find the appropriate syntax or equivalent in javascript.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you're sure that the URL will always have a subdomain (it won't be just abc.com, for example), and that the top-level domain will always consist of one part (it won't be co.uk, for example), you could do this with the following JavaScript:

const exampleURL = "login.xyz.abc.com";
const splitURL = exampleURL.split(".");
const subdomains = splitURL.slice(0, splitURL.length - 2); // Remove the domain
const subdomainsString = subdomains.join(".")
console.log(subdomainsString)

However, keep in mind that the conditions I mentioned above are two pretty big ifs: if an URL is returned as example.co.uk, this method would think that example would be the 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