<p>Doh! It was my ternary logic that was failing not the regex. I’m now doing this:</p>
<pre><code>{% set vidUrl = block.videoUrl %}

{% if 'youtu' in vidUrl %}
    {# Check for an 'embed' URL which may need cleaning #}
    {% set vidId = 'embed' in vidUrl ? vidUrl|replace('/^http.*embed\\/([^\\?]+)(\\?.*)?$/', '$1') : null %}

    {# Check for a 'watch' URL which may need cleaning #}
    {% set vidId = 'watch' in vidUrl ? vidUrl|replace('/^http.*\\?v=([^\\&amp;]+)(\\&amp;.*)?$/', '$1') : vidId %}

    {# Check for a 'youtu.be' URL which may need cleaning #}
    {% set vidId = '.be/' in vidUrl ? vidUrl|replace('/^http.*\\.be\\/([^\\?]+)(\\?.*)?$/', '$1') : vidId %}

    {% set srcUrl = vidId ? &quot;https://www.youtube.com/embed/#{vidId}&quot; : null %}
{% endif %}

{% if srcUrl %}
    {# Write the iframe #}
{% else %}
    {# Write some help for logged in CMS folks #}
{% endif %}
</code></pre>

CC BY-SA 4.0
2021-07-20T12:10:07.543Z
Maybe in your case it's overkill, but a custom Twig variable would be perfect for this job. Also the oEmbed plugin is good at this.
gioppe
CC BY-SA 4.0
2021-07-21T11:43:49.077Z
Thanks @gioppe, [oEmbed](https://plugins.craftcms.com/oembed) does look like a cool plugin. I will check it out.
Jonathan Schofield
CC BY-SA 4.0
2021-07-17T12:48:12.943Z
2021-07-21T11:29:19.543Z
2021-07-21T11:29:19.543Z
Jonathan Schofield
<p>I need to take the dominant YouTube URL schemas and convert them into a YouTube embed URL of the form:</p>
<pre><code>https://www.youtube.com/embed/VIDEOID
</code></pre>
<p>I know there are technically loads of possible YouTube URL variants but I'm really only interested in:</p>
<ol>
<li><code>https://www.youtube.com/watch?v=MvqY2NcBWI8</code></li>
<li><code>https://youtu.be/MvqY2NcBWI8</code></li>
</ol>
<p>…where either of those could be extended with optional parameters.</p>
<p>So I'm doing this to extract the <code>vidId</code>, which works for <strong>1</strong>:</p>
<pre><code>{% set vidUrl = block.videoUrl %}
{% set vidId  = vidUrl|replace('/^http.*\\?v=([^\\&amp;]+)(\\&amp;.*)?$/', '$1') %}
</code></pre>
<p>…and then this for <strong>2</strong>:</p>
<pre><code>{% set vidId = vidId ? vidId : vidUrl|replace('/^http.*\\.be\\/([^\\?]+)(\\?.*)?$/', '$1') %}
</code></pre>
<p>…which <em>doesn’t work</em>.</p>
<p>And yet when I replace <code>\\</code> with <code>\</code> and test it in my text editor it <em>does</em> work.</p>
<p>This should capture the id up to the beginning of any query string…</p>
<pre><code>([^\\?]+)
</code></pre>
<p>…and this should discard any parameters which follow…</p>
<pre><code>(\\?.*)?$
</code></pre>
<p>Shouldn't it? What am I missing?</p>

<filter><regex><replace>
Fixing a YouTube regex in a replace filter
# Fixing a YouTube regex in a replace filter

(Asked by: username_0 on 2021-07-17)

I need to take the dominant YouTube URL schemas and convert them into a YouTube embed URL of the form:
https://www.youtube.com/embed/VIDEOID

I know there are technically loads of possible YouTube URL variants but I'm really only interested in:

https://www.youtube.com/watch?v=MvqY2NcBWI8
https://youtu.be/MvqY2NcBWI8

…where either of those could be extended with optional parameters.
So I'm doing this to extract the vidId, which works for 1:
{% set vidUrl = block.videoUrl %}
{% set vidId  = vidUrl|replace('/^http.*\\?v=([^\\&]+)(\\&.*)?$/', '$1') %}

…and then this for 2:
{% set vidId = vidId ? vidId : vidUrl|replace('/^http.*\\.be\\/([^\\?]+)(\\?.*)?$/', '$1') %}

…which doesn’t work.
And yet when I replace \\ with \ and test it in my text editor it does work.
This should capture the id up to the beginning of any query string…
([^\\?]+)

…and this should discard any parameters which follow…
(\\?.*)?$

Shouldn't it? What am I missing?

**Tags:** filter, regex, replace


---

## 1 answers

### Answer by username_0 on 2021-07-17. Score: 2

Doh! It was my ternary logic that was failing not the regex. I’m now doing this:
{% set vidUrl = block.videoUrl %}

{% if 'youtu' in vidUrl %}
    {# Check for an 'embed' URL which may need cleaning #}
    {% set vidId = 'embed' in vidUrl ? vidUrl|replace('/^http.*embed\\/([^\\?]+)(\\?.*)?$/', '$1') : null %}

    {# Check for a 'watch' URL which may need cleaning #}
    {% set vidId = 'watch' in vidUrl ? vidUrl|replace('/^http.*\\?v=([^\\&]+)(\\&.*)?$/', '$1') : vidId %}

    {# Check for a 'youtu.be' URL which may need cleaning #}
    {% set vidId = '.be/' in vidUrl ? vidUrl|replace('/^http.*\\.be\\/([^\\?]+)(\\?.*)?$/', '$1') : vidId %}

    {% set srcUrl = vidId ? "https://www.youtube.com/embed/#{vidId}" : null %}
{% endif %}

{% if srcUrl %}
    {# Write the iframe #}
{% else %}
    {# Write some help for logged in CMS folks #}
{% endif %}

> **Comments:**
> **username_1**: Maybe in your case it's overkill, but a custom Twig variable would be perfect for this job. Also the oEmbed plugin is good at this.  
> **username_0**: Thanks @username_1, [oEmbed](https://plugins.craftcms.com/oembed) does look like a cool plugin. I will check it out.  

---

