Skip to content

Javascript get the value of an href after a certain pattern

I have a url like I want to capture anything that comes after the en-CA/ (the en-CA can be any combination of characters that have two letters, a dash, and two letters.

I am trying to use a regex to

  1. grab the window.location.href

  2. split the href at the pattern that matched the /en-CA/

  3. take the remainder of the href


I dont think i quite have the right setup here. How do I accomplish grabbing everything after the /character character dash character character /


Here’s my solution

const url = "";
const result = url.split(//[a-z]{2}-[a-z]{2}//i)[1];

if (!result) throw new Error("Invalid url");