<p>You could use the <code>replace</code> filter, it accepts regex:</p>

<p><a href="https://craftcms.com/docs/templating/filters#replace" rel="nofollow">https://craftcms.com/docs/templating/filters#replace</a></p>

CC BY-SA 3.0
2016-07-24T02:17:56.007Z
Thanks for your reply.

I actually tried this but my regex was too weak. Any suggestions for my example?
zuccs
CC BY-SA 3.0
2016-07-22T05:04:47.580Z
2016-07-22T05:04:47.580Z
andrew.welch
<p>Just building on the answer from @khalwat, if you use the <code>replace</code> filter, your solution might look something like this: </p>

<p><code>{{ entry.body | amp | replace(/(&lt;img )(.*?)(\\/&gt;)/g, '&lt;amp-img $2&gt;&lt;/amp-img&gt;') }}
</code></p>

<p>There may be cleaner regex solutions out there, but I think that one will cut the mustard.</p>

CC BY-SA 3.0
2016-08-02T04:30:08.007Z
2016-08-02T05:58:37.943Z
2016-08-02T05:58:37.943Z
nicgordon
<p><a href="https://craftcms.stackexchange.com/questions/12870/approaching-google-amp">This</a> is a great post about how to integrate AMP into your Craft CMS website. </p>

<p>What happens though if you have a standard 'img' tag in your existing content block? </p>

<p>I thought it would be neat to be able to set up a custom Craft/Twig AMP filter that automatically converts: </p>

<pre><code>&lt;img src="/img/logo.svg" alt="Logo" width="150" height="100" /&gt;
</code></pre>

<p>To:</p>

<pre><code>&lt;amp-img src="/img/logo.svg" alt="Logo" width="150" height="100"&gt;&lt;/amp-img&gt;
</code></pre>

<p>By using:</p>

<pre><code>{% block content %} {{ entry.body | amp }} {% endblock %}
</code></pre>

<p>Is there something existing that I could modify to achieve this?</p>

<p>Thanks in advance for any pointers!</p>

<templating><filter>
Google AMP Filter
# Google AMP Filter

(Asked by: username_0 on 2016-07-22)

This is a great post about how to integrate AMP into your Craft CMS website. 
What happens though if you have a standard 'img' tag in your existing content block? 
I thought it would be neat to be able to set up a custom Craft/Twig AMP filter that automatically converts: 


To:


By using:
{% block content %} {{ entry.body | amp }} {% endblock %}

Is there something existing that I could modify to achieve this?
Thanks in advance for any pointers!

**Tags:** templating, filter


---

## 2 answers

### Answer by username_1 on 2016-07-22. Score: 2

You could use the `replace` filter, it accepts regex:

<https://craftcms.com/docs/templating/filters#replace>

> **Comments:**
> **username_0**: Thanks for your reply.  I actually tried this but my regex was too weak. Any suggestions for my example?  

---

### Answer by username_2 on 2016-08-02. Score: 1

Just building on the answer from @khalwat, if you use the `replace` filter, your solution might look something like this:

`{{ entry.body | amp | replace(/(![]())/g, '') }}`

There may be cleaner regex solutions out there, but I think that one will cut the mustard.


---

