简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM