1

I search a way to extract the t time parameters content

So , for example :

https://youtu.be/YykjpeuMNEk?t=2m3s
https://youtu.be/YykjpeuMNEk?t=3s
https://youtu.be/YykjpeuMNEk?t=1h2m3s

I'd like to get the h,m and s values.

I can image i have to use RegEx in order to make the work, but i'm cannot find the right expression string (little novice in that point)

I just have this for the moment :

var matches = t.match(/[0-9]+/g);

i use this tool to test different expressions, but unable to format it correctly and be sure that the content is exactly related to h,m and s.

If you have any idea ;)

ANSWER THAT WORKS FOR ME :

url = 'https://youtu.be/vTs7KXqZRmA?t=2m18s';
var matches = url.match(/\?t=(?:(\d+)h)?(?:(\d+)m)?(\d+)s/i);
var s = 0;
s += matches[1] == undefined ? 0 : (Number(matches[1])*60*60);
s += matches[2] == undefined ? 0 : (Number(matches[2])*60);
s += matches[3] == undefined ? 0 : (Number(matches[3]));
console.log(s);

output :

138

thanks to all ;)

4
  • regex101.com/r/rR3eH9/1 Commented Mar 4, 2016 at 18:20
  • Thanks @WiktorStribiżew , works like a charm ! Commented Mar 4, 2016 at 20:09
  • 1
    I guess anubhava's works too? If not, I will post my answer. Commented Mar 4, 2016 at 20:14
  • yes, thanks @WiktorStribiżew , it was the nearly the same way and it works ;) cheers ! Commented Mar 4, 2016 at 22:59

1 Answer 1

2

You can use this regex to captre h, m and s values with h and m as optional parts:

/\?t=(?:(\d+)h)?(?:(\d+)m)?(\d+)s/i

RegEx Demo

RegEx Breakup:

\?t=        # match literal text ?t=
(?:         # start capturing group
   (\d+)h   # match a number followed by h and capture it as group #1
)?          # end optional capturing group
(?:         # start capturing group
   (\d+)m   # match a number followed by m and capture it as group #2
)?          # end optional capturing group
(\d+)s      # # match a number followed by s and capture it as group #3
Sign up to request clarification or add additional context in comments.

1 Comment

Woow... nice.. works perfectly ! thansk a lot @anubhava :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.