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

151
Visualizações
Regex that only matches when 4 specific words were found

As the title says, I need a Regex to check if a string has this four words (Update, Rollback, Skip, Not Now) and only return them if all of them are present, if not it doesn’t return anything.

Here is an example: {"Update":"iVBORw0KGgo","Rollback":"iVBORw0KGgo","Skip":"iVBORw0KGgo","Not Now":"iVBORw0KGgo"}

In this case, it should return [Update, Rollback, Skip, Not Now]

{"Update":"iVBORw0KGgo","Skip":"iVBORw0KGgo","Not Now":"iVBORw0KGgo"}

In this case, it shouldn’t return any value

I tried to create one by myself but my knowledge of Regex is very basic: (Update|Rollback|Skip|Not Now)

Thanks in advance!


EDIT

I noticed that Regex might not be the best way to achieve this.

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

0

As an alternative to regex, you can use JSON.parse to parse the string into an object and Object.keys to get the properties:

const str = `{"Update":"iVBORw0KGgo","Rollback":"iVBORw0KGgo","Skip":"iVBORw0KGgo","Not Now":"iVBORw0KGgo"}`;

const keys = Object.keys(JSON.parse(str))

const result = keys.sort().toString() == "Not Now,Rollback,Skip,Update" ? keys : "";

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

While regex is clearly not a good tool for the job, you can do something like this:

re.match("(?=.*Update.*)(?=.*Skip.*)",string)

"(?=WORD)" matches if the expression follows, but doesn't consume any of the string.

Of course, complete the regex by all 4 words that you want similarly.

Also notice that regex by itself doesn't return anything. You need to code for this.

about 4 years ago · Juan Pablo Isaza Relatório

0

Use this:

^(?=.*"Update"\s*:)(?=.*"Rollback"\s*:)(?=.*"Skip"\s*:)(?=.*"Not Now"\s*:)

(?=...) means that if you lookahead, you find this pattern.

So an empty match will return as soon as we find all these 4 patterns.

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