Extracts the YouTube video ID from a url or string:
YouTubeVideoId(string)
NPM:
$ npm install youtube-video-id
Yarn:
$ yarn add youtube-video-id
CDN:
<script src="https://unpkg.com/youtube-video-id@latest/dist/youtube-video-id.min.js"></script>
Server:
var YouTubeVideoId = require('youtube-video-id');
// returns video ID
YouTubeVideoId('https://www.youtube.com/watch?v=X3pTXG9a1oQ'); // 'X3pTXG9a1oQ'
YouTubeVideoId('https://youtu.be/X3pTXG9a1oQ'); // 'X3pTXG9a1oQ'
YouTubeVideoId('X3pTXG9a1oQ'); // 'X3pTXG9a1oQ'
Client:
<script src="https://unpkg.com/youtube-video-id@latest/dist/youtube-video-id.min.js"></script>
<script>
var videoId = YouTubeVideoId('https://www.youtube.com/watch?v=F2uovvU-dLA')
console.log(videoId);
</script>
Also see JSFiddle and other examples.
$ npm test
$ npm run lint