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

509
Visualizações
javascript regex to find multiple strings in a line preceded by a key:

How can I match all the strings, in any order, but only if preceded by certain key?

example I want to match article and legal no matter the order as long as they are preceded by tags:

---
author: Karen the Trollmaster
tags: noise, legal, irrelevant, article
keywords: pff, nobody, likes, "a Karen", "not this", article // < don't match this one
---

What I have so far is

(?<=(tags: ))(article)|(legal)

but this isn't working correctly.

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

0

I would do this in two steps, first isolate the tags: line, then use match() to find all keywords with an alternation regex.

var input = `---
author: Karen the Trollmaster
tags: noise, legal, irrelevant, article
keywords: pff, nobody, likes, "a Karen", "not this", article // < don't match this one
---`;
var keywords = ["article", "legal"];
var regexp = new RegExp("\\b(" + keywords.join("|") + ")\\b", "g");
var matches = input.match(/\btags:.*?(?=\n|$)/)[0].match(regexp);
console.log(matches);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can write the pattern as:

(?<=tags: .*)\b(?:article|legal)\b

The pattern matches:

  • (?<= Positive lookbehind, assert to the left from the current position
    • tags: .* Match tags: and 0+ times any
  • ) Close lookbehind
  • \b(?:article|legal)\b Match either article or legal between word boundaries

Regex demo

Note that you can omit the capture groups for a match only

const regex = /(?<=tags: .*)\b(?:article|legal)\b/gm;
const str = `---
author: Karen the Trollmaster
tags: noise, legal, irrelevant, article
keywords: pff, nobody, likes, "a Karen", "not this", article // < don't match this one
---`;

console.log(str.match(regex));

If both words should be present, you can make use of lookarounds to match either of the words and assert the other word to be present on the left or the right side

(?<=tags: .*)(?:\blegal\b(?=.*\barticle\b)|\barticle\b(?=.*\blegal\b)|(?<=.*\barticle\b.*)\blegal\b|(?<=.*\blegal\b.*)\barticle\b)

Regex demo

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