Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!