Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this regular expression part add?

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?

like image 693
pimvdb Avatar asked Aug 19 '11 18:08

pimvdb


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What does this regular expression do?

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.

How do you add expressions in regex?

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).


1 Answers

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 = ''
like image 86
Jacob Eggers Avatar answered Oct 06 '22 17:10

Jacob Eggers