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?
$ means "Match the end of the string" (the position after the last character in the string).
Regular expressions are useful in search and replace operations. The typical use case is to look for a sub-string that matches a pattern and replace it with something else. Most APIs using regular expressions allow you to reference capture groups from the search pattern in the replacement string.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
The two regexes would match the same strings, but would store different information in their capturing groups.
for the string: mozilla asdf rv:sadf
/(mozilla)(?:.*? rv:([\w.]+))?/
$0 = 'mozilla asdf rv:sadf'
$1 = 'mozilla'
$2 = 'sadf'
/(mozilla)/
$0 = 'mozilla'
$1 = 'mozilla'
$2 = ''
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With