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

161
Visualizações
RegExp capturing non-match

I have a regex for a game that should match strings in the form of go [anything] or [cardinal direction], and capture either the [anything] or the [cardinal direction]. For example, the following would match:

go north go foo north

And the following would not match:

foo go

I was able to do this using two separate regexes: /^(?:go (.+))$/ to match the first case, and /^(north|east|south|west)$/ to match the second case. I tried to combine the regexes to be /^(?:go (.+))|(north|east|south|west)$/. The regex matches all of my test cases correctly, but it doesn't correctly capture for the second case. I tried plugging the regex into RegExr and noticed that even though the first case wasn't being matched against, it was still being captured.

How can I correct this?

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

0

Try using the positive lookbehind feature to find the word "go".

(north|east|south|west|(?<=go ).+)$

Note that this solution prevents you from including ^ at the start of the regex, because the text "go" is not actually included in the group.

about 4 years ago · Juan Pablo Isaza Relatório

0

You have to move the closing parenthesis to the end of the pattern to have both patterns between anchors, or else you would allow a match before one of the cardinal directions and it would still capture the cardinal direction at the end of the string.

Then in the JavaScript you can check for the group 1 or group 2 value.

^(?:go (.+)|(north|east|south|west))$
                                   ^  

Regex demo

Using a lookbehind assertion (if supported), you might also get a match only instead of capture groups.

In that case, you can match the rest of the line, asserting go to the left at the start of the string, or match only 1 of the cardinal directions:

(?<=^go ).+|^(?:north|east|south|west)$

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