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

201
Visualizações
How to extract a specific substring (based on regex) from an array of strings

I have a big array of strings:

['{{Wanted Value}}', 'true', '{{Wanted Value}}', '{{Wanted Value}} unwanted {{Wanted Value}}', 'false'...]

I want to filter the array and extract every {{Wanted Value}} substring to a new array. If the item in the array contains 2 or more of these substrings, I want to have each of them as a separate item. So the result of the above array would be:

['{{Wanted Value}}', {{Wanted Value}}', {{Wanted Value}}', {{Wanted Value}}']

I wrote the regex I want to use but not sure how to write the filter function correctly:

match(/\{\{(.+?)\}\}/)[0]

Thank you

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

0

You can try this approach

  • flatMap is to collect all matched strings
  • filter is to get rid of empty results

const data =['{{Wanted Value}}', 'true', '{{Wanted Value}}', '{{Wanted Value}} unwanted {{Wanted Value}}', 'false']

const result = data.flatMap(stringData => stringData.match(/\{\{(.+?)\}\}/g)).filter(stringData => stringData);

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is a code which use reduce and string match function to filter the result

let r = ['{{Wanted Value}}', 'true', '{{Wanted Value}}', '{{Wanted Value}}', 'unwanted','{{Wanted Value}}', 'false'];

const result = r.reduce((accumulator, current) => {
  return current.match(/\{\{(.+?)\}\}/)? accumulator.concat(current): accumulator;
}, []);

console.log(result);

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