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

139
Visualizações
regex to find part of string and associated value

I have a string that looks like this:

const string = 'test_string%name=peter&age=18&foo=bar&lol=loli_copter';

I want to write a regex to get name and the age from this string and their values.

const result = 'name=peter&age=18;

I have tried the following but with no luck:

const result = string.match(/(?<=name\s+).*?(?=\s+age)/gs);

Can anyone help or point me in the right direction?

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

0

We can use string match here along with a regex pattern which targets the name and age keys, along with their RHS values.

var string = 'test_string%name=peter&age=18&foo=bar&lol=loli_copter';
var parts = string.match(/\b(?:name|age)=[^&]+/g);
var output = parts.join('&');
console.log(output);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could just match "any characters except &" for name and "any number of digits" for age. e.g.

name=([^&]+)&age=(\d+)

const string = 'test_string%name=peter&age=18&foo=bar&lol=loli_copter';
const data = string.match(/name=([^&]+)&age=(\d+)/);

console.log(data[1], data[2]) // peter 18

about 4 years ago · Juan Pablo Isaza Relatório

0

const string = 'test_string%name=peter&age=18&foo=bar&lol=loli_copter';
const name = string.match(/name=([^&]*)/)[1];
const age = string.match(/age=([^&]*)/)[1];

console.log(name, age)

Output: peter 18

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