It can be tricky if you're new to it.
.
normally means any non-space character. In a range ([]
), however, it reverts to its literal meaning, i.e. a full stop (period, if you're American.)
^
normally means "anchor to the start of the string." In a range, however, when it's the first character in that range, it flips the logic, so rather than the range representing the allowed characters, it represents the DISallowed characters.
So the pattern '/[^.]+\.[^.]+$/'
says:
- Match a sequence of one or more non-space characters that are not periods (the host)
- Match a period thereafter
- Match another sequence of one or more non-space characters that are not periods
(the suffix)
- The
$
anchors this to the end of the string, so steps 1-3 must be in sequence right up to the last character of the string.
Incidentally, it's not exactly a water-tight pattern for hostname matching. It doesn't take into account sub-domains or country-specific domains with more than one period (e.g. ".co.uk"), to name two points.