0

I'm using this regex for turning a youtube link into embedded code:

str.replace(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?)?(?:.+)v=(.+)/g, '<iframe width="420" height="345" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>');

But it doesn't work when youtube link has more than the v-parameter, like

http://www.youtube.com/watch?v=iXVe-efsYWw&feature=g-feat

Can anybody help me to change the regex for cutting any additional parameters off?

David 10K
  • 303
  • 2
  • 14
  • 1
    What have you tried? You need to show some research into the matter, we're not here just to do your job for you. – lanzz Sep 04 '12 at 13:06
  • Possible duplicate: http://stackoverflow.com/questions/2597080/regex-to-parse-youtube-yid?rq=1 – aquinas Sep 04 '12 at 13:07
  • This solved my problem: [php regex find all youtube video ids in string](http://stackoverflow.com/questions/5830387/php-regex-find-all-youtube-video-ids-in-string/5831191#5831191) – David 10K Sep 04 '12 at 14:24

1 Answers1

0

The best regex I did for that is this one:

~^.*(?:youtu\.be\/|youtube\.com\/(?:[^\?]*\?(?:.*&)?v=|(?:embed|v|user)\/))([^\?&"\'>]{11}).*$~si

Replace it for group 1. (\1 or $1)

Hope it helps. Worked for every youtube URL I got. It don't work for any let me know and I can make it better.