2024-12-01 10:00:01

URL中允许的字符

有人知道可以在GET中使用而不经过编码的完整字符列表吗?目前我使用A-Z - A-Z和0-9…但我想知道完整的名单。

我也感兴趣的是,是否会发布关于即将添加中文、阿拉伯语url的规范(显然这将对我的问题产生很大影响)。


当前回答

66个无保留字符的完整列表在RFC3986中,请访问:https://www.rfc-editor.org/rfc/rfc3986#section-2.3

这是以下正则表达式集中的任何字符:

[A-Za-z0-9_.\-~]

其他回答

这些都列在RFC3986中。请参阅收集的ABNF中的URI,以了解在哪里允许什么,以及用于解析/验证的正则表达式。

RFC3986定义了两组你可以在URI中使用的字符:

Reserved Characters: :/?#[]@!$&'()*+,;= reserved = gen-delims / sub-delims gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" The purpose of reserved characters is to provide a set of delimiting characters that are distinguishable from other data within a URI. URIs that differ in the replacement of a reserved character with its corresponding percent-encoded octet are not equivalent. Unreserved Characters: A-Za-z0-9-_.~ unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" Characters that are allowed in a URI but do not have a reserved purpose are called unreserved.

从这里

因此,只有字母数字,特殊字符$-_.+!*'(), 以及用于their的保留字符 保留目的可以在URL中未经编码使用。

这个答案讨论了字符可以包含在URL片段部分中而不进行转义。我单独发布了一个答案,因为这部分与这里的其他优秀答案略有不同(并且可以结合使用)。

片段部分不会被发送到服务器,在这个例子中,它是在#后面的字符:

https://example.com/#STUFF-HERE

规范

RFC 3986中的相关规范为:

  fragment    = *( pchar / "/" / "?" )
  pchar       = unreserved / pct-encoded / sub-delims / ":" / "@"
  unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
  sub-delims  = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="

这也引用了RFC 2234中的规则

  ALPHA       =  %x41-5A / %x61-7A   ; A-Z / a-z
  DIGIT       =  %x30-39             ; 0-9

结果

所以完整的列表,不包括转义(pct编码)是:

A-Z -z 0-9。_ ~ !$ & ' () * +,;=: @ / ?

为了方便起见,这里有一个匹配有效的、未转义的片段的PCRE表达式:

/^[A-Za-z0-9\-._~!$&'()*+,;=:@\/?]*$/

编码

算起来,有:

26 + 26 + 10 + 19 = 81代码点

您可以使用基数81来有效地编码这里的数据。

66个无保留字符的完整列表在RFC3986中,请访问:https://www.rfc-editor.org/rfc/rfc3986#section-2.3

这是以下正则表达式集中的任何字符:

[A-Za-z0-9_.\-~]