0

I have a javascript code that will look for any YouTube link on the page and will replace those links with iFrame and embed the videos in the iFrame.

This work fine.

But sometimes, the youtube links have some stuff at the end of the link like this:

&t=3s or &t=44s etc

I need to be able to remove those as well but I can't fine a way to do this.

This is my code:

https://jsfiddle.net/6tv4uebw/

Code:

$(".indiContent").html(function (e, t) {
    return t.replace(/http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?/g, '<iframe width="100%" height="200" src="https://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>')
});

Couls someone please advice on this issue?

Thanks in advance.

James Juanjie
  • 219
  • 3
  • 18

2 Answers2

0

You can use this:

t.replace(/http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?/g, '<iframe width="100%" height="200" src="https://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>').replace(/&[^]*/,'')

jsfiddle: jsfiddle.net/hrps0e5m/

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jannis
  • 41
  • 1
  • 7
0

The &t=3s in the end of the url is a parameter passed by url. You can find all of them with simple regex.

Before the return (in your code), add the following lines:

var regex = /&.*/;
t = t.replace(regex, "");

And then do whatever you want with the link without these parameters.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
SeReGa
  • 1,219
  • 2
  • 11
  • 32