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?