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

99
Visualizações
Javascript regex to match a list of specific words in any order

I have sentences like 'On the green and purple and yellow and red grass', or 'On the green and purple and yellow and red and yellow and green and green grass', or 'On the green grass', and I want to match them to a regex and extract the specific words ('green', 'purple', 'yellow', 'red') from them. I tried the following:

const regex = /On the (green|purple|yellow|red)( and (green|purple|yellow|red))* grass/

So, like, match the first word (which should always be there), and then ' and [..]' any number of times.

So then I do this: 'On the green and green and green grass'.match(regex)

But the results are:

'On the green and green and green grass',
'green',
' and green',
'green',

And for On the green and green and green and purple and purple and yellow grass

it's

'On the green and green and green and purple and purple and yellow grass',
'green',
' and yellow',
'yellow',

So this gets the first word and the last word. How do I get all the words? That is, from the first example I want to extract [green, green, green], and from the second [green, green, green, purple, purple, yellow]. I don't want to match/extract anything from On the green and red and black grass because black isn't in the list of allowed words. In addition, the sentence is specific: it starts with On the and ends with grass. I was wondering if it's possible to check all of this with a single regex.

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

0

Your question isn't really clear but assuming you want to extract the colours, simply grouping them along with the OR boolean and a global flag should work fine:

const regex = /(green|red|purple|yellow)/g;

const str1 = "On the red and green and green and green and purple and purple and yellow grass";
const str2 = "On the green and green and green grass";

console.log(str1.match(regex));
console.log(str2.match(regex));

about 4 years ago · Juan Pablo Isaza Relatório

0

If you only want to match the colours in that particular sentence, you can avoid the issue of only getting the last match of the capture group by using lookarounds instead, for example:

const text = 'On the green and green and green and purple and purple and yellow grass';
const colors = '(?:green|purple|yellow|red)';

const regex = new RegExp(
  `(?<=On the (?:${colors} and )*)${colors}(?=(?: and ${colors})* grass)`,
'g');

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

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