<p>Actually, excluding certain tags from being removed is built into the <code>striptags</code> <a href="https://twig.symfony.com/doc/2.x/filters/striptags.html" rel="nofollow noreferrer">filter</a>.</p>

<p>You can use it like this:</p>

<pre><code>{{ articleTextTrim | striptags('&lt;a&gt;') }}
</code></pre>

CC BY-SA 4.0
2018-05-15T15:46:24.730Z
but then it outputs as html like <a href="www.example.com">link to something</a> I was hope to keep the "link to something" text but remove the html markup.
Danielle Frappier
CC BY-SA 4.0
2019-07-10T21:22:31.390Z
A raw filer should stop escaping the link. 

{{ articleTextTrim | striptags('<a>') | raw }}
Willhaus
CC BY-SA 4.0
2018-05-14T15:02:34.597Z
2018-05-14T15:02:34.597Z
Jalen Davenport
<p>I have a rich text (redactor field) that I'm using striptags on to remove the html markup from the front end. However I noticed for links it whips out the link and the text in between. Is this something I can fix with a regular expression? Can you even do that with a twig filter? If so does anyone have an example?</p>

<p>Here is my code: </p>

<pre><code>{% set articleText = myFirstMatrixBlockWithType.blogArticleText %}
{% if articleText | length &gt; 200 %}
    {% set articleTextTrim = articleText | slice(0, 200) ~ '...' %}
    {{ articleTextTrim | striptags }}
{% else %}
    {{ articleText | striptags }} 
{% endif %}
</code></pre>

<templating><richtext><regex>
striptags but leave link test in between
# striptags but leave link test in between

(Asked by: username_0 on 2018-05-14)

I have a rich text (redactor field) that I'm using striptags on to remove the html markup from the front end. However I noticed for links it whips out the link and the text in between. Is this something I can fix with a regular expression? Can you even do that with a twig filter? If so does anyone have an example?
Here is my code: 
{% set articleText = myFirstMatrixBlockWithType.blogArticleText %}
{% if articleText | length > 200 %}
    {% set articleTextTrim = articleText | slice(0, 200) ~ '...' %}
    {{ articleTextTrim | striptags }}
{% else %}
    {{ articleText | striptags }} 
{% endif %}

**Tags:** templating, richtext, regex


---

## 1 answers

### Answer by username_1 on 2018-05-14. Score: 1

Actually, excluding certain tags from being removed is built into the striptags filter.
You can use it like this:
{{ articleTextTrim | striptags('') }}

> **Comments:**
> **username_0**: but then it outputs as html like <a href="www.example.com">link to something</a> I was hope to keep the "link to something" text but remove the html markup.  
> **username_2**: A raw filer should stop escaping the link.   {{ articleTextTrim | striptags('<a>') | raw }}  

---

