const regex = /(https?:\/\/)?(www.)?((player.vimeo.com\/video\/[0-9]{9})|((youtube.com|youtu\.be)\/embed\/([\w-]{9,11})))/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(https?:\\\/\\\/)?(www.)?((player.vimeo.com\\\/video\\\/[0-9]{9})|((youtube.com|youtu\\.be)\\\/embed\\\/([\\w-]{9,11})))', 'gm')
const str = `http://www.youtube.com/watch?v=GKnQGP67Xd0
https://www.youtube.com/watch?v=GKnQGP67Xd0
https://www.youtube.com/watch?v=GKnQGP67Xd0
https://www.youtube.com/watch?v=GKnQGP67Xd0
https://vimeo.com/662065137
https://vimeo.com/662065137
https://www.youtube.com/c/riotgamesmusic
https://vimeo.com/662065137
https://youtu.be/aXUCHNaZkWo
https://vimeo.com/662065137
https://vimeo.com/baconbaconbacon
https://www.youtube.com/channel/UCG_wKjFPEpdHf1Q6bTdE2JA
https://player.vimeo.com/video/662065137
última formato corto: http://youtu.be/NLqAF9hrVbY
iframe: http://www.youtube.com/embed/NLqAF9hrVbY
iframe (seguro): https://www.youtube.com/embed/NLqAF9hrVbY
objeto param: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
objeto de incrustación: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
ver: http://www.youtube.com/watch?v=NLqAF9hrVbY
usuarios: http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
ytscreeningroom: http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
cualquier cosa vale!: http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4
cualquier/subdominio/: http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY
más params: http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec
la consulta puede tener el punto: http://www.youtube.com/watch?v=spDj54kf-vY&feature=youtu.be`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions