Let's explain the regex:
-
/.../
- These /
are used in Javascript to denote that what is inside them is a regex. So, the real regex is ^(?:(?:gatos?|cachorro):)?
.
-
^
- Beginning of the string. This means that whatever is found, has to be found at the beginning of the string, not in the middle of it.
-
(?: ... )
- This is a group without capture. It is used only for grouping subexpressions. Regex allow captures with ( ... )
, so you can extract parts of the text that gives match . Using this ?:
disables capture when you are not interested in it or when it might mess up other parts in which you want the capture to take place. In your case, you have no interest in capturing parts, just the whole.
-
gatos?|cachorro
- This can be gato
or gatos
or cachorro
. s?
means s
may or may not appear. The |
indicates alternatives. The (?: ... )
around is used to group this together so that |
knows where the alternatives start and ends.
-
:
(the last) - Means the character :
itself.
-
The last ?
- means that what is before (?: ... )
may or may not appear. If it does not appear, it is still given match.
In this way, there are only four strings that this regex recognizes and that should be docked at the beginning of the string. These are:
-
(empty)
-
gato:
-
gatos:
-
cachorro:
Note that the end of the string is not checked. Therefore, the use of gato:blabla
also gives a match. But since the start is checked, xgato:
does not match.
I suppose the original context looks something like this:
\^(?:(?:https?|ftp):)?\
That is, it has something to do with checking if a piece of text is a link (whether it starts with http:
, https:
or ftp:
). However, he still accepts the case that there is none of this because of the last ?
.