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

273
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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