EN
HTML - characters that should be escaped
10
points
In this short article, we would like to count what characters should be escaped in HTML.
Quick solution:
REPLACED ALWAYS:
& should be replaced with &
< should be replaced with <
> should be replaced with >
REPLACED CONDITIONALLY in element attribute (depending on the case):
" should be replaced with " e.g. <div attribute="abc " abc"></div>
' should be replaced with ' e.g. <div attribute='abc ' abc'></div>
Detailed description
Character | HTML escape code | Description |
& | & | It is required to escape this character because it is used as an escape character, e.g. < , > , " , ' , etc. |
< | < | Opens HTML elements. |
> | > | Closes HTML elements. |
Additionally, inside of attribute values, it is necessary to escape the quote character:
Character | HTML escape code | Description |
" | " |
Needed only when
|
' | ' |
Needed only when
|
Practical example
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<p title=""John" life story ...">Few words about John life story...</p>
<a href='https://john.com' title='John's site'>Home</a>
<p>Source code should be placed between <code> and </code>.</p>
</body>
</html>