I came across this regular expression in the jQuery source code:
...
rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
...
I was wondering why it was rather complicated. I’m especially interested in the reason behind the second part:
(?:.*? rv:([\w.]+))?
I did some research but I could not figure out what this part of the regular expression adds.
(?:) to match but not capture
.*? any amount of any character
rv: something literal
([\w.]+) one or more word characters or a dot
? appear 0 or 1 time
Particularly, that last ? doesn’t make much sense to me. The whole second part matches if there is or is not a substring as defined by that second part. With some trial and error the regular expression does not seem to differ from just:
/(mozilla)/
Could someone shed some light on what the second part of the regular expression is supposed to do? What does it constrain; what string fails that passes /(mozilla)/ or the other way round?
The two regexes would match the same strings, but would store different information in their capturing groups.
for the string:
mozilla asdf rv:sadf