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

127
Visualizações
JS regex match zero or more optional start with key words

Given string like keydown.capture.once.prevent.shift.control[arrowdown,arrowup]:silent I want to match shift.control[arrowdown,arrowup]:silent

And if the same keydown.capture.once.prevent.shift[arrowdown,arrowup]:silent then it should match shift[arrowdown,arrowup]:silent

Here the keywords shift | control are optional, ex: keydown.capture.once.prevent.[arrowdown,arrowup]:silent, matching string [arrowdown,arrowup]:silen

I wrote the below regex, it can only capture zero or one keyword, but the expected result is zero or all keywords matching into separate group

(shift|control)?\[(.*?)\]:silent, how can we capture all the keywords if they exist?

Additional notes: Order of keywords doesn't matter, ex it can be control.shift[]:silent

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

0

You may use any one of these regex solutions:

(?:(?:shift|control)\.?)*\[([^\]]*)\]:silent

This matches shift or control` optionally followed by a dit and repeats this non-capture group 0 or more times.

RegEx Demo

(?:shift(?:\.control)?|control(?:\.shift)?)?\[([^\]]*)\]:silent

This one matches shift.control OR control.shift in an optional non-capture group with the parts after dot optional in each alternation.

RegEx Demo 2

about 4 years ago · Juan Pablo Isaza Relatório

0

You could try this:

(?:(shift|control|shift\.control|control\.shift))?(\[.*\]):silent

Test and output: https://regex101.com/r/rMSd2l/1

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