Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-24T02:17:56.007Z
Answers.Text: Thanks for your reply.
  
  I actually tried this but my regex was too weak. Any suggestions for my example?
Answers.UserDisplayName: zuccs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-22T05:04:47.580Z
Answers.LastActivityDate: 2016-07-22T05:04:47.580Z
Answers.UserDisplayName: andrew.welch
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-02T04:30:08.007Z
Answers.LastActivityDate: 2016-08-02T05:58:37.943Z
Answers.LastEditDate: 2016-08-02T05:58:37.943Z
Answers.UserDisplayName: nicgordon
Body: <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>
  
Tags: <templating><filter>
Title: Google AMP Filter
ThreadText: # Google AMP Filter
  
  (Asked by: username_0 on 2016-07-22)
  
  [This](https://craftcms.stackexchange.com/questions/12870/approaching-google-amp) 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:
  
  ```
  <img src="/img/logo.svg" alt="Logo" width="150" height="100" />
  
  ```
  
  To:
  
  ```
  <amp-img src="/img/logo.svg" alt="Logo" width="150" height="100"></amp-img>
  
  ```
  
  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(/(<img )(.*?)(\\/>)/g, '<amp-img $2></amp-img>') }}`
  
  There may be cleaner regex solutions out there, but I think that one will cut the mustard.
  
  
  ---
  
  