URL fragment (#) allowed characters
Asked Answered
I

2

32

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/
Istanbul answered 28/9, 2014 at 19:37 Comment(4)
ietf.org/rfc/rfc3986.txtWenger
Thank you bmargulies, but I'm still unable to find the valid characters for URL fragment on that page.Istanbul
See 2.5 and 3.5. The later ends by saying that anything, even / and ?, can occur.Wenger
To me it only says that characters / and ? are allowed. (The characters slash ("/") and question mark ("?") are allowed to represent data within the fragment identifier.)Istanbul
P
61

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 =
Parliamentary answered 30/9, 2014 at 10:59 Comment(4)
A regex for valid characters in the fragment identifier component would be very useful here :-) Can anyone provide this?Dolan
^([-?/:@._~!$&'()*+,;=a-zA-Z0-9]|%[0-9a-fA-F]{2})*$ (Some escaping might be required depending on your regex dialect.)Insurrection
i noticed that "#" isn't listed as being valid in a fragment, although it seems browsers are lenient at allowing multiple ones of them...Sherilyn
Note that when using ":" in IDs, you need to escape it when using querySelector as \\:.Belanger
I
2

It's a bit tricky to find the valid characters, but the file commented above does contain the information if you read deep enough.

The available characters are as follow:

  • "!"
  • "$"
  • "&"
  • "'"
  • "("
  • ")"
  • "*"
  • "+"
  • ","
  • ";"
  • "="
  • "?"
  • "@"
  • ALPHA
  • DIGIT
  • "-"
  • "."
  • "_"
  • "~"
  • "%" HEXDIG HEXDIG
  • ":"
  • "/"
Istanbul answered 28/9, 2014 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.