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

269
Vistas
How do I split a website URL with multiple separators in JavaScript?

I'm trying to create a Custom JavaScript Variable in Google Tag Manager to split up information from a page url that has multiple separators. For example, in https://website.com/item/2006-yellow-submarine, I would want to capture the 2006. I've been using the code below to separate a URL based on one separator at a time (- or /). But if I used the code below to pull 2006, it would pull 2006 and everything after (so the data pulled would be 2006-yellow-submarine and not just 2006).

function() {
  var pageUrl = window.location.href;
  return pageUrl.split("/")[4];
}

Is there a way to extract only the 2006, or to essentially use a combination of - and / separators to pull single path points from a URL, without having to specify the URL in the code each time?

Since it's a variable meant to be used to automatically capture the year on each page, I can't make a variable for every individual page of the website. Therefore the solution can't involve specifying the URL.

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

0

You can split by RegExp, splitting by either / or -, i.e. /[/-]/:

const u = new URL("https://website.com/item/2006-yellow-submarine");
const pathname = u.pathname;
console.log(pathname);
// skip the first item... it will always be empty
const [, ...pathParts] = pathname.split(/[/-]/);
console.log(pathParts);

about 4 years ago · Juan Pablo Isaza Denunciar

0

To do this you could do this:

function splitWebURL(url, loc1, loc2) {
  return url.split("/")[loc1].split("-")[loc2];
}
var test = splitWebURL("https://website.com/item/2006-yellow-submarine", 4, 0);
console.log(test);

It works great and you can change it with ease by just changing the 2 index numbers and changing the url.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Make a new URL object from the string, extract the last part of the pathname, and then match the year with a small regular expression.

// Create a new URL object from the string
const url = new URL('https://website.com/item/2006-yellow-submarine');

// `split` the pathname on "/", and `pop` off
// the last element of that array
const end = url.pathname.split('/').pop();

// Then match the year
console.log(end.match(/[0-9]{4}/)[0]);

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