Subject: HTML Escapes (and too much info about this stuff)
Author:
Posted on: 2015-06-22 04:28:00 UTC

Basically, in HTML, the characters <, >, and & are (usually) special. The browser interprets them as part of the "markup", which is instructions for how the text of the page should be displayed or what it means. For example, if you put some words between <b&gt and </b>, they become bold because the <b> stuff gets read as tags that mean "the things between these are bold".

This system is nice because it lets you put things like bold or links into otherwise ordinary text, but it has the problem of making it seemingly impossibly to write about the tags, since, if you just put them in your document, the browser will interpret them and not show them to the person reading the page. If there wasn't a way around this, it would be impossible to write an HTML tutorial in HTML.

Fortunately, the creators of HTML have given us an escape hatch. It works like this: the string &[stuff]; is also special. It doesn't get displayed as-is either. Instead, the [stuff] between the & and the ; is used to find out which otherwise-special character you were trying to put in your page. Then, he escape is replaced by the character it represents when the page is presented to the user. (Escapes are also used to deal with encoding issues, but that's another story).

The three most useful escapes are:


  • &lt;, which represents a <

  • &gt;, which represents a >

  • &amp;, which represents an &


(Note that, in order for me to tell you the escapes, I had to escape them. So, to show &amp;, I had to write &amp;amp;, and so on. Isn't recursion fun!)

This is actually not the most common strategy for dealing with special characters. Many other computery things, like regular expressions, pick a particular character (almost always ) to mean "flip the specialness of the next character". If you put a \ in front of a normally special character, it becomes non-special, and if you put one in front of (some) normal characters, they represent some different, special thing.

Reply Return to messages