-3

So I'm wanting to figure out how do you exclude on some numbers from a string input. For example, If I were to input this "https://www.shoepalace.com/products/jordan-dr6287-486-air-jordan-5-low-doernbecher-mens-lifestyle-shoes-blue-multi?variant=41679925772494", I'd want "?variant=41679925772494" remove and keep the rest the same. I can't use a constant of "?variant=41679925772494" because I'm wanting it work across all string inputs, not the same one. what I have is below, but it removes all numbers within the link, rather than whats after "?variant=". Does anyone have any ideas on how I can go about this?

let updatedLink = link.replace("?variant=", "")
let variantLink = updatedLink.replace(/[0-9]/g, "")```
JediSZN
  • 59
  • 6

1 Answers1

1

If you want to parse URL's, using regex is not really the best option.

Browsers have the URL class that does exactly what your after.

eg.

var n = new URL('https://www.shoepalace.com/products/jordan-dr6287-486-air-jordan-5-low-doernbecher-mens-lifestyle-shoes-blue-multi?variant=41679925772494');

n.searchParams.delete('variant');

console.log(n.toString());
Keith
  • 22,005
  • 2
  • 27
  • 44