0

I want to find each youtube link in a page. I found on StackOverflow some regex i modified but when i have a html code with two youtube linq the result is one match like

youtube.com?v=videoid<div></div>youtube.com?v=videoid2

I want to get each youtube link only.

My regex is :

/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&amp;]v=)|youtu\.be\/)([\w|-|_]{11})/

Can someone help me please?

Thanks and sorry for bad english.

Pascal Mormin
  • 206
  • 2
  • 8

1 Answers1

0

try to add 'g' modifier at the end of the regular expression like so:

/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&amp;]v=)|youtu\.be\/)([\w|-|_]{11})/g

That means globally (get all matches)

erwan
  • 887
  • 8
  • 17
  • I doubt you really mean to use `[?&]` as it matches either `?` or `&`, or `a`, or `m`, or `p` or `;`. – Wiktor Stribiżew Jun 08 '16 at 10:46
  • i mean to use the /g modifier in order to get all occurences of the regular expression matches. The rest of the regex come from Pascal Mormin, i just copy / pasted it to give a full fonctionnal example – erwan Jun 08 '16 at 10:48
  • `[\w|-|_]` im pretty sure this should be `[\w-_]` as otherwise it matches alphanumeric, _, -, | – yosefrow Jun 08 '16 at 10:53
  • Thanks for your answers. I chose to add /g and split the string. It seems to be good. mystring.match(/(https?:\/\/)?(www\.)?(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|(e(?:mbed)?)|watch)\/|.*v=)|youtu\.be\/)([\w-_)]{11})/g)[0].split(/(?:<[^<>]+>)+/) – Pascal Mormin Jun 08 '16 at 14:52