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.

3
  • input.split(/(?:<[^<>]+>)+/) Commented Jun 8, 2016 at 10:27
  • Thank you Wiktor and sorry for duplicate answer Commented Jun 8, 2016 at 14:54
  • I decided to give you one because you actually tried something and no need to be sorry for a dupe. Commented Jun 8, 2016 at 15:04

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

4 Comments

I doubt you really mean to use [?&amp;] as it matches either ? or &, or a, or m, or p or ;.
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
[\w|-|_] im pretty sure this should be [\w-_] as otherwise it matches alphanumeric, _, -, |
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(/(?:<[^<>]+>)+/)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.