Problem: I have a string, e.g.: "to see to be to read"
and I'd like to capture the 3 verbs without the "to " prefix, in this case: be
, see
and read
.
On Regex 101, I've tried this really simple regex and it solved the problem:
Regex: /to (\w+)/g
Result: ['be', 'see', 'read']
Just for curiosity, I've made this another regex, using positive lookahead, and the results were the same.
Regex: /(?=to \w+)\w+ (\w+)/g
Result: ['be', 'see', 'read']
Okay. Weird thing is: When I'm running this regex on Browser Console (either Chrome or Firefox), the results are different. The two following tries gives me the same results: all three groups including the to
prefix.
> 'to be to see to read'.match(/to (\w+)/g)
["to be", "to see", "to read"]
> 'to be to see to read'.match(/(?=to \w+)\w+ (\w+)/g)
["to be", "to see", "to read"]
Am I missing something here or am I stepping on a bug?
Disclaimer: This is not homework, I'm just validating this for a bigger problem. I'm not a regex expert but know a thing or two about it.
EDIT: I think I was fooled by Regex101. The code sample it gave me showed the String#match()
approach, but this function doesn't exclude regexp groups accordingly on the resulting groups. Looping over RegExp#exec()
matches is the way to go!