Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!