Creating an a href link to a URL which contains square brackets causes Stack Overflow to ignore the link. According to this post, there were similar problems in markdown, but I can't see this addressed about HTML links.

<a href="http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#binarySearch%28byte[],%20byte%29">binarySearch</a>

Produces binarySearch, which is not a link.


This is by design, we require that the URLs linked from post bodies actually be URLs. Cleaning up HTML is already tricky enough without trying to make "technically wrong but probably works" stuff pass through.

But never fear, if you have a URL like http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#binarySearch(byte[], byte) you can get a working link by using the link button in the post editor (also accessible with CTRL-L).

Raw <a>, doesn't work

Using the toolbar, it works

Look at the source to see how we generate proper links.

  • Thanks, that's very useful to know! – Zarkonnen Oct 17 '12 at 7:40
  • Doesn't work in comments, though... – MirroredFate Feb 27 '14 at 0:28

It appears that this is an invalid URL. See also Which characters make a URL invalid? (which is slightly different, since here the [] characters are in the fragment identifier rather than the path component of the URL.

Looking at the RFC, the relevant definitions are:

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

If I'm reading the grammar correctly, this means that [ and ] are not permitted in the fragment identifier, which makes your URL invalid.

Given that the URL is invalid, this is the correct behavior.

  • But by using <a href> you are specifically saying that you want this to be treated as a URL. I understand there's ambiguity if the URL is "naked", but this behavior breaks expectations of how HTML works. It makes it hard to copy-paste links to Java documentation. If the issue is that this would allow for weird non-working URLs to be added, the URL fruithammer.com/92380 is valid but will nevertheless 404. – Zarkonnen Oct 12 '12 at 7:41
  • Or putting it another way, I understand that the URL is technically invalid, but it's also unambiguous and works in practice. – Zarkonnen Oct 12 '12 at 7:43
  • 1
    Or, putting it another way, it's not a URL (since it does not fit the definition of a URL). It just mostly looks like one. – yoozer8 Oct 12 '12 at 12:51
  • Sure, but it works as an URL, and it's something you can find all over Javadoc, a major type of programming resource. Of course this can be fixed up by escaping the [], but you have to know how to do that in the first place. I'm not trying to argue semantics, I'm arguing usability. – Zarkonnen Oct 12 '12 at 13:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .