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

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

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

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

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