Answers.Body: <p>You could use Twig's <a href="http://twig.sensiolabs.org/doc/filters/replace.html" rel="nofollow">replace filter</a> on output:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{{ entry.richtext_field_handle|replace('&amp;': '&amp;amp;') }}
  </code></pre>
  
  <p>At first glance I couldn't find a <a href="http://imperavi.com/redactor/docs/settings/" rel="nofollow">Redactor setting</a> which would help you, but you might want to check yourself, or even write a <a href="http://imperavi.com/redactor/docs/plugins-creating/" rel="nofollow">Redactor plugin</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-08T12:59:03.510Z
Answers.LastActivityDate: 2014-08-08T12:59:03.510Z
Answers.UserDisplayName: Victor
Answers.Body: <p>I finally found a way to do that using the "<a href="https://github.com/victor-in/Craft-TwigPCRE" rel="nofollow noreferrer">TwigPCRE</a>" Craft plugin:</p>
  
  <pre><code>|preg_replace('/&amp;(?!(?:[a-z]+|#x?\d+);)/', '&amp;amp;')|raw
  </code></pre>
  
  <p>This will replace all unescaped &amp; characters which are not part of something else.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-11T07:13:24.843Z
Answers.LastActivityDate: 2017-05-24T07:37:48.847Z
Answers.LastEditDate: 2017-05-24T07:37:48.847Z
Answers.UserDisplayName: Andre Mohren
Answers.Body: <p><strong>You don't have to escape the ampersand for it to be valid html5</strong>, and if it's appearing in content, not in a URL for example, then you really have nothing to worry about.</p>
  
  <p>This validates:</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;head&gt;
    &lt;title&gt;E&amp;K&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;h1&gt;E&amp;K&lt;/h1&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>
  <a href="https://i.sstatic.net/FbyLA.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/FbyLA.png" alt="enter image description here"></a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-24T11:16:34.537Z
Answers.LastActivityDate: 2017-05-24T11:16:34.537Z
Answers.UserDisplayName: Seth Warburton
Body: <p>I'm currently finishing a client-website which has to be 100% valid HTML5. For one of the sections, I am using a matrix containing rich-text fields. This content is simply shown on the front page.
  The name of the client is "E&amp;K".</p>
  
  <p>Requirements:</p>
  
  <ul>
  <li>The output contains HTML tags.</li>
  <li>The output has to be 100% valid HTML.</li>
  </ul>
  
  <p>Problem:
  The &amp; character won't be escaped to &amp;amp; wherever it is outputted. This breaks the valid requirement. It seems the problem is the input, not the output. However the rich text editor keeps replacing manually placed &amp;amp; with &amp;</p>
  
  <p>How can I get this to work correctly?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-07-24T15:54:01.767Z
Comments.Text: If an answer provided the solution to your question it's good etiquette, and good manners, to mark it as the correct answer.
Comments.UserDisplayName: Seth Warburton
Tags: <richtext><escaping>
Title: How to use "&amp;" in richtext?
ThreadText: # How to use "&amp;" in richtext?
  
  (Asked by: username_0 on 2014-08-08)
  
  I'm currently finishing a client-website which has to be 100% valid HTML5. For one of the sections, I am using a matrix containing rich-text fields. This content is simply shown on the front page.
  The name of the client is "E&K".
  
  Requirements:
  
  * The output contains HTML tags.
  * The output has to be 100% valid HTML.
  
  Problem:
  The & character won't be escaped to &amp; wherever it is outputted. This breaks the valid requirement. It seems the problem is the input, not the output. However the rich text editor keeps replacing manually placed &amp; with &
  
  How can I get this to work correctly?
  
  **Tags:** richtext, escaping
  
  > **Comments:**
  > **username_1**: If an answer provided the solution to your question it's good etiquette, and good manners, to mark it as the correct answer.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_2 on 2014-08-08. Score: 2
  
  You could use Twig's [replace filter](http://twig.sensiolabs.org/doc/filters/replace.html) on output:
  
  ```twig
  {{ entry.richtext_field_handle|replace('&': '&amp;') }}
  
  ```
  
  At first glance I couldn't find a [Redactor setting](http://imperavi.com/redactor/docs/settings/) which would help you, but you might want to check yourself, or even write a [Redactor plugin](http://imperavi.com/redactor/docs/plugins-creating/).
  
  
  ---
  
  ### Answer by username_0 on 2014-08-11. Score: 1
  
  I finally found a way to do that using the "[TwigPCRE](https://github.com/username_2-in/Craft-TwigPCRE)" Craft plugin:
  
  ```
  |preg_replace('/&(?!(?:[a-z]+|#x?\d+);)/', '&amp;')|raw
  
  ```
  
  This will replace all unescaped & characters which are not part of something else.
  
  
  ---
  
  ### Answer by username_1 on 2017-05-24. Score: 0
  
  **You don't have to escape the ampersand for it to be valid html5**, and if it's appearing in content, not in a URL for example, then you really have nothing to worry about.
  
  This validates:
  
  ```
  <!doctype html>
  <head>
    <title>E&K</title>
  </head>
  <body>
   <h1>E&K</h1>
  </body>
  
  ```
  
  [![enter image description here](https://i.sstatic.net/FbyLA.png)](https://i.sstatic.net/FbyLA.png)
  
  
  ---
  
  