I have a chat app and I am trying to do it so that every time a person sends a link to YouTube it will create an embedded Iframe instead.
I created a function which has regex inside that suppose to locate an href with YouTube link and return the YouTube id, I use this regex to replace all YouTube links to embedded iframe with the correct id.
It works, unless there is more than one link in the html input (In this case it will replace all the links and all of the things between them into one iframe with the id of the last link).
This is an example:
let convertYoutubeLinksToIframe = function (html) {
let response = html;
try {
const findLinksRegex = /<a(?:[^]+href="(?:http(?:s)?:\/\/(?:m.)?(?:www\.)?youtu(?:\.be\/|be\.com\/(?:watch\?(?:feature=youtu.be\&)?v=|v\/|embed\/|user\/(?:[\w#]+\/)+))([^&#?\n]+)[^]+)">[^]+<\/a>?)+/gmi;
response = html.replace(findLinksRegex , '<iframe style="max-width: 100%; min-height: 160px;" src="https://www.youtube.com/embed/$1" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>');
} catch (err) {
return html;
}
return response;
}
let htmlExample = `<div><a href="http://www.youtube.com/watch?v=iwGFalTRHDA"></a>
<a href="http://www.youtube.com/watch?v=bdOBjlHJFJA&feature=related"></a>
<a href="http://youtu.be/ikjJllTKKHK"></a>
<a href="http://youtu.be/n17B_uFF4cA"></a>
<p>This is a paragraph</p>
<a href="http://www.youtube.com/watch?v=njZPH8544sc"></a>
<a href="http://youtu.be/nbJRX655sc"></a>
<a href="https://youtu.be/2sFlFPmUfNo?t=1"></a></div>`
console.log(convertYoutubeLinksToIframe(htmlExample));
If you run this code you'll see that You get only one iframe inside the div and not one frame every link as I want to get. Also the paragraph in the middle also get's deleted.
This is my expected output:
<div>
<iframe style="max-width: 100%; min-height: 160px;" src="https://www.youtube.com/embed/iwGFalTRHDA" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<iframe style="max-width: 100%; min-height: 160px;" src="https://www.youtube.com/embed/bdOBjlHJFJA" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<iframe style="max-width: 100%; min-height: 160px;" src="https://www.youtube.com/embed/ikjJllTKKHK" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<iframe style="max-width: 100%; min-height: 160px;" src="https://www.youtube.com/embed/n17B_uFF4cA" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<p>This is a paragraph</p>
<iframe style="max-width: 100%; min-height: 160px;" src="https://www.youtube.com/embed/njZPH8544sc" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<iframe style="max-width: 100%; min-height: 160px;" src="https://www.youtube.com/embed/nbJRX655sc" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<iframe style="max-width: 100%; min-height: 160px;" src="https://www.youtube.com/embed/2sFlFPmUfNo" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
Can any one help me solve this problem?