1

I have the following regex that I'm using in Javascript:

/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/

It gets me the video ID correctly. I want to expand it to extract start time of videos:

I know there are at least two formats (if not more): "t=" and "start="

http://youtu.be/xxxxxxxx?t=30s

https://www.youtube.com/watch?v=xxxxxxxx&start=30

thanks,

Kamy D
  • 1,071
  • 2
  • 13
  • 23
  • I had seen that post already when I was searching. I didn't want all that extra code when I'm already using regex to retrieve video id. I wanted to be able to expand the current regex. thx – Kamy D Feb 22 '15 at 16:23

2 Answers2

5

Seems like you want something like this,

^.*?(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*)(?:(\?t|&start)=(\d+))?.*

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 1
    Thanks, it works!! "t" can be either "?t" or "&t", so I added the OR to your answer, here is what I ended up with: ^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*)(?:(\?t|\&t|&start)=(\d+))?.* and can satisfy all these: http://youtu.be/xxxxxxxx?t=30s https://www.youtube.com/watch?v=xxxxxxxx&start=30 https://www.youtube.com/watch?v=xxxxxxxx&t=70 – Kamy D Feb 22 '15 at 17:02
0

Try this way for javascript

   var re = /(t=|start=)(?:\d+)/g;
    var str = 'http://youtu.be/xxxxxxxx?t=30s\n\nhttps://www.youtube.com/watch?v=xxxxxxxx&start=30';
    var m;

    while ((m = re.exec(str)) != null) {
    if (m.index === re.lastIndex) {
    re.lastIndex++;
    }
    // View your result using the m-variable.
    alert(eg m[0])
    }

enter image description here

DEMO

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103