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

285
Views
How to get value of URL query parameter with dynamic name?

I need to extract the value of a query parameter in a URL, but the parameter changes on each page.

For example, I want to get the color variable, but it always changes based on the productID. In this case it is 'dwvar_2000440926_color' but for another product it will be 'dwvar_545240926_color'. _color stays consistent, so I'd like to key off that:

https://www.example.com/us/2000440926.html?dwvar_2000440926_color=02

Thanks!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You should use regex. Based on the description of the URL behavior you described you could do something like this:

const url = new URL("https://www.example.com/us/2000440926.html?dwvar_2000440926_color=02");

// Now url.search contains your query parameters.
// We gonna apply the regex on it to capturing the color id
let matches = url.search.match(/dwvar_\d+_color=(\d+)/)

// `matches` now contains the captured groups
console.log(matches[1])
// log : 02

about 4 years ago · Juan Pablo Isaza Report

0

Assuming that 1) you want to do this on the client side 2) the color param always begins with dwvar as shown in your example and 3) that there is never more than one dwvar param, you can use the following javascript:

let searchParams = new URLSearchParams(document.location.search);
searchParams.forEach((param_value, param_name) => {
  if (param_name.indexOf('dwvar') == 0) {
    console.log(param_value)
  }
})
about 4 years ago · Juan Pablo Isaza Report

0

Basic regular expression would work

const myURL = new URL("https://www.example.com/us/2000440926.html?dwvar_2000440926_color=02")

console.log(myURL.search.match(/_color=([^&]+)/)[1]);

// more specfic
console.log(myURL.search.match(/dwvar_\d+_color=([^&]+)/)[1]);

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!