Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
26
rated 0 times [  33] [ 7]  / answers: 1 / hits: 58618  / 11 Years ago, thu, october 24, 2013, 12:00:00

I have a regex pattern that I'm using (got it from Stack Overflow) to extract a video ID from a vimeo URL:



var regExp = /http://(www.)?vimeo.com/(d+)($|/)/;
var match = url.match(regExp);


I need it to work whether http or https is specified. I've tried



var regExp = /http(s)?://(www.)?vimeo.com/(d+)($|/)/;


But this fails on both http and https.



Help a brother out.


More From » regex

 Answers
101

It fails because you are creating an extra capturing group, meaning that the capturing group indexes will not be the same as before.



To make the s optionnal without creating a capturing group, you can simply add ?, you do not need the parenthesis.



/https?://(www.)?vimeo.com/(d+)($|/)/


To create a non-capturing group, you can use (?:), but that's not necessary here, just showing for the example:



/http(?:s)?://(www.)?vimeo.com/(d+)($|/)/

[#74760] Wednesday, October 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reed

Total Points: 725
Total Questions: 85
Total Answers: 89

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;