Answers.Body: <p>I don't think you can - by hand is the only way I've found!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-18T15:41:30.003Z
Answers.Text: Thx, your are right. I posted my "by hand" solution as answer.
Answers.UserDisplayName: Mario
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-18T10:56:09.527Z
Answers.LastActivityDate: 2017-01-18T10:56:09.527Z
Answers.UserDisplayName: darylknight
Answers.Body: <p>I ended up using this solution:</p>
  
  <h3>Filter empty tags in Craft/Twig template</h3>
  
  <pre><code>{{ entry.houseDescription|replace('/&lt;[^\\/&gt;][^&gt;]*&gt;&lt;\\/[^&gt;]+&gt;/', '')|raw }}
  </code></pre>
  
  <ol>
  <li>The <a href="https://craftcms.com/docs/templating/filters#replace" rel="nofollow noreferrer"><code>replace</code></a> filter <code>/&lt;[^\\/&gt;][^&gt;]*&gt;&lt;\\/[^&gt;]+&gt;/</code> searches for empty tags and removes them.</li>
  <li>The <a href="http://twig.sensiolabs.org/doc/2.x/filters/raw.html" rel="nofollow noreferrer"><code>raw</code></a> filter stops Twig from escaping HTML tags.</li>
  </ol>
  
  <p><a href="http://regexr.com/3f3dp" rel="nofollow noreferrer">Regular expression test (RegExr)</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-18T13:12:35.117Z
Answers.LastActivityDate: 2017-01-18T13:20:24.377Z
Answers.LastEditDate: 2017-01-18T13:20:24.377Z
Answers.UserDisplayName: Mario
Body: <p>What is the preferred method to remove an empty tag from the content of a rich text element?</p>
  
  <p>Example result:</p>
  
  <pre><code>&lt;p&gt;
    Lorem&lt;a href="…"&gt;&lt;/a&gt; ipsum
  &lt;/p&gt;
  </code></pre>
  
  <p>When editing text in the WYSIWYG editor (Redactor) the result can contain empty anchor tags. This is a problem when navigating the page with the tab key, for instance, because the user can not see the focus anymore.</p>
  
  <p>How can I automatically remove those artefacts?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-01-13T15:56:35.893Z
Comments.Text: one way is to allow HTML in the redactor config and check it over for these things, but that's not great or automatic. You could do something like *:empty{display:none} in the css but that's not great either (as a workmate of mine said, that's just sweeping the turd under the rug)
Comments.UserDisplayName: billythekid
Tags: <templating><redactor>
Title: How to remove empty tags in rich text? (Redactor or Twig)
ThreadText: # How to remove empty tags in rich text? (Redactor or Twig)
  
  (Asked by: username_0 on 2017-01-13)
  
  What is the preferred method to remove an empty tag from the content of a rich text element?
  
  Example result:
  
  ```
  <p>
    Lorem<a href="…"></a> ipsum
  </p>
  
  ```
  
  When editing text in the WYSIWYG editor (Redactor) the result can contain empty anchor tags. This is a problem when navigating the page with the tab key, for instance, because the user can not see the focus anymore.
  
  How can I automatically remove those artefacts?
  
  **Tags:** templating, redactor
  
  > **Comments:**
  > **username_1**: one way is to allow HTML in the redactor config and check it over for these things, but that's not great or automatic. You could do something like *:empty{display:none} in the css but that's not great either (as a workmate of mine said, that's just sweeping the turd under the rug)  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2017-01-18. Score: 4 | Accepted answer
  
  I ended up using this solution:
  
  ### Filter empty tags in Craft/Twig template
  
  ```
  {{ entry.houseDescription|replace('/<[^\\/>][^>]*><\\/[^>]+>/', '')|raw }}
  
  ```
  
  1. The [`replace`](https://craftcms.com/docs/templating/filters#replace) filter `/<[^\\/>][^>]*><\\/[^>]+>/` searches for empty tags and removes them.
  2. The [`raw`](http://twig.sensiolabs.org/doc/2.x/filters/raw.html) filter stops Twig from escaping HTML tags.
  
  [Regular expression test (RegExr)](http://regexr.com/3f3dp)
  
  
  ---
  
  ### Answer by username_2 on 2017-01-18. Score: 0
  
  I don't think you can - by hand is the only way I've found!
  
  > **Comments:**
  > **username_0**: Thx, your are right. I posted my "by hand" solution as answer.  
  
  ---
  
  