const regex = /http(?:s)?:\/\/(?:www\.)?youtube\.com\/.*?watch%3Fv%([\d\w-_]+)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('http(?:s)?:\\\/\\\/(?:www\\.)?youtube\\.com\\\/.*?watch%3Fv%([\\d\\w-_]+)', 'g')
const str = `http://youtube.com/attribution_link?a=r_FgjB-RKNo&u=/watch%3Fv%3DB0_X8zWJd0Q%26feature%3Dem-uploademail
http://youtube.com/attribution_link?a=1crTn2Lx38I&u=/watch%3Fv%3DHhebhh2s5d4%26feature%3Dem-uploademail
http://youtube.com/attribution_link?a=_zMgiznzBto&u=/watch%3Fv%3D-kaoCPvUHJM%26feature%3Dem-uploademail
http://youtube.com/attribution_link?a=F8VReY1sT0A&u=/watch%3Fv%3D5RLM9rk0Q8s%26feature%3Dem-uploademail
https://www.youtube.com/watch?feature=player_detailpage&v=2eGUP16P_p4
`;
// 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