URL 片段 (#) 允许的字符 [英] URL fragment (#) allowed characters

查看:18
本文介绍了URL 片段 (#) 允许的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在互联网上进行了一些挖掘之后,我无法找到一个很好的答案来说明我可以将哪些字符用于 URL 片段.我正在编写一个将利用 URL 片段的 javascript 脚本.

我想让 URL 看起来不那么复杂,从而对眼睛友好.所以我想知道我是否可以使用像 ':, ?, & 这样的字符.或者 !'在 URL 片段中并且仍然有效.

我的 URL 片段应包含以下值:

  • 按顺序
    • id
    • descasc
  • 路径
    • /the/full/escaped/path/here/

解决方案

tl;dr

片段标识符组件可以包含:

  • 0 - 9
  • a - z
  • A - Z
  • ? / : @ - . _ ~ ! $ & '( ) * + , ; =
  • 百分比编码字符(一个 % 后跟两个十六进制数字)

我怎样才能找到这个?

URI 标准是 STD 66,目前映射到 RFC 3986.

在本文档中,您将找到您需要了解的一切.

片段标识符组件在部分中定义3.5:

<块引用>

fragment = *( pchar/"/"/"?" )

这意味着片段可以不包含任何内容或(任意组合)

  • pchar
  • 中定义的字符
  • /
  • ?

pchar

的定义

请参阅附录 A. 了解如何pchar 已定义:

<块引用>

pchar = unreserved/pct-encoded/sub-delims/:";/"@"

所以增加了

  • 未保留
  • 中定义的字符
  • pct-encoded中定义的字符
  • 子分隔符
  • 中定义的字符
  • :
  • @

unreserved的定义

现在检查 unreserved 是如何定义的:

<块引用>

unreserved = ALPHA/DIGIT/-"/."/_"/"~"

这个补充

  • ALPHA 中定义的字符
  • DIGIT 中定义的字符
  • -
  • .
  • _
  • ~

ALPHADIGIT的定义

检查 ALPHA 和 DIGIT 是如何定义的.它们未在附录中列出,因为它们来自核心 ABNF 规则,正如 在第 1.3 节中解释:

<块引用>

ALPHA(字母),[...] DIGIT(十进制数字)[...]

所以增加了

  • a-z, A-Z
  • 0-9

pct-encoded的定义

检查 pct-encoded 是如何定义的:

<块引用>

pct-encoded = "%";HEXDIG HEXDIG

这允许任何百分比编码字符.>

sub-delims的定义

检查 sub-delims 是如何定义的:

<块引用>

sub-delims = "!";/$";/&"/'";/"("/")";/*"/+"/,"/;"/="

这个补充

  • !
  • $
  • &
  • '
  • (
  • )
  • *
  • +
  • ,
  • ;
  • =

After some digging on the internet I was unable to find a good answer to which characters I may use for URL fragment. I'm writing a javascript-script that will take advantage of URL fragments.

I wanted to make the URL eye-friendly by not having it looking too complicated. So I was wondering if I could use characters like ':, ?, & or !' in the URL fragment and still have it valid.

My URL fragment should contain the following values:

  • order-by
    • id
    • desc or asc
  • path
    • /the/full/escaped/path/here/

解决方案

tl;dr

The fragment identifier component can contain:

  • 0 - 9
  • a - z
  • A - Z
  • ? / : @ - . _ ~ ! $ & ' ( ) * + , ; =
  • percent-encoded characters (a % followed by two hexadecimal digits)

How can I find this out?

The URI standard is STD 66, which currently maps to RFC 3986.

In this document, you’ll find everything you need to know.

The fragment identifier component is defined in section 3.5:

fragment = *( pchar / "/" / "?" )

This means that the fragment can contain nothing or (any combination of)

  • characters defined in pchar
  • the /
  • the ?

Definition of pchar

Refer to the appendix A. to see how pchar is defined:

pchar = unreserved / pct-encoded / sub-delims / ":" / "@"

So this adds

  • characters defined in unreserved
  • characters defined in pct-encoded
  • characters defined in sub-delims
  • the :
  • the @

Definition of unreserved

Now check how unreserved is defined:

unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"

This adds

  • characters defined in ALPHA
  • characters defined in DIGIT
  • the -
  • the .
  • the _
  • the ~

Definition of ALPHA and DIGIT

Check how ALPHA and DIGIT are defined. They are not listed in the appendix, because they are from the core ABNF rules, as is explained in section 1.3:

ALPHA (letters), […] DIGIT (decimal digits) […]

So this adds

  • a-z, A-Z
  • 0-9

Definition of pct-encoded

Check how pct-encoded is defined:

pct-encoded = "%" HEXDIG HEXDIG

This allows for any percent-encoded character.

Definition of sub-delims

Check how sub-delims is defined:

sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="

This adds

  • the !
  • the $
  • the &
  • the '
  • the (
  • the )
  • the *
  • the +
  • the ,
  • the ;
  • the =

这篇关于URL 片段 (#) 允许的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆