Last active
March 7, 2016 06:16
-
-
Save aarestu/7c66d311d44ca06e10c8 to your computer and use it in GitHub Desktop.
find all Youtube video ids in a string using a regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
'YouTube ID: $1'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function linkifyYouTubeURLs($text) { | |
$text = preg_replace('~(?#!js YouTubeId Rev:20160125_1800) | |
# Match non-linked youtube URL in the wild. (Rev:20130823) | |
https?:// # Required scheme. Either http or https. | |
(?:[0-9A-Z-]+\.)? # Optional subdomain. | |
(?: # Group host alternatives. | |
youtu\.be/ # Either youtu.be, | |
| youtube # or youtube.com or | |
(?:-nocookie)? # youtube-nocookie.com | |
\.com # followed by | |
\S*? # Allow anything up to VIDEO_ID, | |
[^\w\s-] # but char before ID is non-ID char. | |
) # End host alternatives. | |
([\w-]{11}) # $1: VIDEO_ID is exactly 11 chars. | |
(?=[^\w-]|$) # Assert next char is non-ID or EOS. | |
(?! # Assert URL is not pre-linked. | |
[?=&+%\w.-]* # Allow URL (query) remainder. | |
(?: # Group pre-linked alternatives. | |
[\'"][^<>]*> # Either inside a start tag, | |
| </a> # or inside <a> element text contents. | |
) # End recognized pre-linked alts. | |
) # End negative lookahead assertion. | |
[?=&+%\w.-]* # Consume any URL (query) remainder. | |
~ix', 'YouTube ID : $1', | |
$text); | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment