I\'m using this to convert youtube url to embed url.
text(t).html().replace(/(?:http:\\/\\/)?(?:www\\.)?(?:youtube\\.com)\\/(?:watch\\?v=)?(.+)/g, \'
To anyone looking at this in 2020, you can get the embed code by using the oembed API.
https://www.youtube.com/oembed?url=&format=
example:
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=gBrmnB5aOSI&format=json
The response you will get is
{
"type": "video",
"thumbnail_width": 480,
"provider_name": "YouTube",
"title": "Intro To Live Streaming on YouTube",
"thumbnail_height": 360,
"provider_url": "https://www.youtube.com/",
"version": "1.0",
"height": 270,
"author_name": "YouTube Creators",
"html": "",
"author_url": "https://www.youtube.com/user/creatoracademy",
"width": 480,
"thumbnail_url": "https://i.ytimg.com/vi/gBrmnB5aOSI/hqdefault.jpg"
}
you can use the html data for the iframe