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

462
Visualizações
JS: Can someone explain me this regex and is it possible to write without regex exp?

I do not have experience with regex, I found this in code and I am trying to understand it. This is regexp: (/[a-z\d]+=[a-z\d]+/gi); And context in which is used is next:

const queryString = window.location.search;
let matches = queryString.match(/[a-z\d]+=[a-z\d]+/gi);
let count = matches ? matches.length : 0;

Also, I am interested is this possible to write in a different way, not to use regexp?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It's checking how many query strings are in the url.

/[a-z\d]+=[a-z\d] says any letter or digit between an = and another letter or digit should match.

The gi part means it is global case insensitive.

  • g = global, match all instances of the pattern in a string, not just one
  • i = case-insensitive (so, for example, /a/i will match the string "a" or "A".

const urlQueryString = 'https://test.com/page?name=ferret&color=purple'
let matches = urlQueryString.match(/[a-z\d]+=[a-z\d]+/gi);
let count = matches ? matches.length : 0;

console.log(count) // --> 2 name=ferret color=purple

There is a tool available here allowing you to test the regex.

about 4 years ago · Juan Pablo Isaza Relatório

0

This code will count amount of query parameters present in string. However, you can use URLSearchParams class for counting entries in query strings.

// URL: https://example.com/?query1=value&query2=value
const searchParams = new URLSearchParams(window.location.search);

// This row will map iterator into array
// [ ['query1', 'value'], ['query2', 'value'] ]
const entriesArray = [...searchParams.entries()];

// 2
console.log(entriesArray.length);
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