var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
Why is the dot . in this part [0-9.-A-Za-z]+ not escaped by a backslash?
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
Why is the dot . in this part [0-9.-A-Za-z]+ not escaped by a backslash?
Brackets ([]
) specify a character class: matching a single character in the string between []
.
While inside a character class, only the \
and -
have special meaning (are metacharacters):
\
: general escape character.-
: character range.
[0-9]
means any number between 0
and 9
, while in [09-]
, -
assumes the quality of an ordinary -
, not a range.That's why, inside []
, a .
is just (will only match) a dot.
Note: It is also worth noticing that the char ]
must be escaped to be used inside a character class, such as [a-z\]]
, otherwise it will close it as usual. Finally, using ^
, as in [^a-z]
, designates a negated character class, that means any char that is not one of those (in the example, any char that is not a...z
).
So it matches a dot.
Except under some circumstances (e.g., escaping the range hyphen when it's not the first character in the character class brackets) you don't need to escape special characters in a class.
You may escape the normal metacharacters inside character classes, but it's noisy and redundant.