Reputation: 54949
I'm trying to replace a youtube link within HTML, just that link and nothing else, with the youtube embed.
Example here:
https://regex101.com/r/5JhmpW/4
(youtu.*be.*)\/(watch\?v=|embed\/|v|shorts|)(.*?((?=[&#?])))
<p>http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index</p><p>Para</p>
<p>http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o</p><p>Para</p>
<p>http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0</p><p>Para</p>
<p>http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s</p><p>Para</p>
<p>http://www.youtube.com/embed/0zM3nApSvMg?rel=0</p><p>Para</p>
<p>http://www.youtube.com/watch?v=0zM3nApSvMg</p><p>Para</p>
<p>http://youtu.be/0zM3nApSvMg</p><p>Para</p>
<p>https://youtube.com/shorts/0dPkkQeRwTI?feature=share</p><p>Para</p>
<p>https://youtube.com/shorts/0dPkkQeRwTI</p><p>Para</p>
<p>https://www.youtube.com/watch?v=0dPkkQeRwTI&ab_channel=RelaxedChan</p><p>Para</p>
The problems currently are that it doesn't match enough to replace everything and I couldn't get it to go to the end of the link without interfering with the markup.
Edit:
They should all look like this:
<p><iframe width="100%" src="https://www.youtube.com/embed/0zM3nApSvMg" frameborder="0" allowfullscreen></iframe><iframe width="100%" src="https://www.youtube.com/embed/0dPkkQeRwTI" frameborder="0" allowfullscreen></iframe></p><p>Para</p>
<p><iframe width="100%" src="https://www.youtube.com/embed/QdK8U-VIH_o" frameborder="0" allowfullscreen></iframe></p><p>Para</p>
<p><iframe width="100%" src="https://www.youtube.com/embed/0zM3nApSvMg" frameborder="0" allowfullscreen></iframe></p><p>Para</p>
<p><iframe width="100%" src="https://www.youtube.com/embed/0zM3nApSvMg" frameborder="0" allowfullscreen></iframe></p><p>Para</p>
<p><iframe width="100%" src="https://www.youtube.com/embed/0zM3nApSvMg" frameborder="0" allowfullscreen></iframe></p><p>Para</p>
<p><iframe width="100%" src="https://www.youtube.com/embed/0zM3nApSvMg" frameborder="0" allowfullscreen></iframe></p><p>Para</p>
<p><br><iframe width="100%" src="https://www.youtube.com/embed/0zM3nApSvMg" frameborder="0" allowfullscreen></iframe></p><p>Para</p>
<div><iframe width="100%" src="https://www.youtube.com/embed/0dPkkQeRwTI" frameborder="0" allowfullscreen></iframe></div><p>Para</p>
<p><iframe width="100%" src="https://www.youtube.com/embed/0dPkkQeRwTI" frameborder="0" allowfullscreen></iframe></p><p>Para</p>
<p><iframe width="100%" src="https://www.youtube.com/embed/0dPkkQeRwTI" frameborder="0" allowfullscreen></iframe></p><p>Para</p>
Upvotes: 1
Views: 342
Reputation: 166
How about this:
http.+(?:(?:v=)|(?:\/a\/u\/1\/)|(?:(?:v|embed|youtu\.be|shorts)\/))(?<videoId>.{11})[^<]*
https://regex101.com/r/oaS7U5/1
Upvotes: 1