你好,我在这里找到了很多线程,这说明了如何从url中解析youtube id,但是没有找到任何可以从字符串中提取youtube视频id的线程。
我需要一个可以从url返回youtube视频id的函数。
例如,我有字符串
> Hello this is my string and this is my youtube video : http://youtube.com/w=abcdef and this is my blog http://stackoverflow.com etc etc.它应该会回来
> Hello this is my string and this is my youtube video : abcdef and this is my blog http://stackoverflow.com etc etc.尝试这个函数但不能正常工作
function linkifyYouTubeURLs(text) {
var re = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig;
return text.replace(re,
'<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>');
}发布于 2015-04-22 05:24:43
试试这个:
var re = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i;https://stackoverflow.com/questions/29788349
复制相似问题