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.
Adding ? after the non-capturing group makes the whole non-capturing group optional. Alternatively, you could do: \".
asterisk or star ( * ) - matches the preceding token zero or more times. For example, the regular expression ' to* ' would match words containing the letter 't' and strings such as 'it', 'to' and 'too', because the preceding token is the single character 'o', which can appear zero times in a matching expression.
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+)($|\/)/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With