-1
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?

4

2 回答 2

2

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

  • backslash \: general escape character.
  • hyphen -: character range.
    • Notice, though, it must be between chars to have special meaning:
      • [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).

于 2013-10-08T20:42:03.110 回答
1

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.

于 2013-10-08T20:41:02.017 回答