Possible Duplicate:
php regex - find all youtube video ids in string
I'm looking for a function that will allow me to change all Youtube links to Javascript function call with ID.
I will show you my code, but it's wrong... not working as i want...:
function find_and_replace_youtube_links(message)
{
return message.replace(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g, 'play_youtube(\'$1\')');
}
function play_youtube(youtube_id)
{
$('#youtube_embed').html('something youtubeid something');
}
The calling
alert(find_and_replace_youtube_links('test \n\
http://www.youtube.com/watch?v=JGCsyshUU&feature=autoplay&list=sSPDBAE171ADC2D71FA&playnext=1\n\
\n\
testt http://www.youtube.com/watch?v=JGCsyshUU&feature=autoplay&list=sSPDBAE171ADC2D71FA&playnext=1 testt\n\
\n\
testtt'));
And output
test
play_youtube('JGCsyshUU&feature=autoplay&list=sSPDBAE171ADC2D71FA&playnext=1')
testt play_youtube('JGCsyshUU&feature=autoplay&list=sSPDBAE171ADC2D71FA&playnext=1 testt')
testtt
Hmmm...
The problem is here:
play_youtube('JGCsyshUU&feature=autoplay&list=sSPDBAE171ADC2D71FA&playnext=1')
You can see, that it parse all link, not just id It should give me
play_youtube('JGCsyshUU');
instead of
play_youtube('JGCsyshUU&feature=autoplay&list=sSPDBAE171ADC2D71FA&playnext=1')
with is wrong
:-(
Anyone doing something similar and have working function? I can adapt to my requirements but need to have something in common with mine requirements.