I am trying to extract video id using regex. Here are some video url examples:
http://fast.wistia.net/embed/iframe/26sk4lmiix
https://support.wistia.com/medias/26sk4lmiix
My regex:
var rx = /https?://(.+)?(wistia.com|wi.st)/(medias|embed)/*/([^;?]+)/,
_m = url.match(rx)
if(_m && _m[4])return _m[4]
else return false;
Regiex is not quite correct and is returning for exmaple:
iframe/26sk4lmiix
3
Answers
If you just want to extract the last portion of the URL path component via regex, then just match on the patttern
[^/]+$
:You could also split the URL on forward slash, and retain the final component:
Here is the code without using the regex pattern.
Output of url1:
26sk4lmiix
Output of url2:
26sk4lmiix
If you want to keep your current group patterns intact, you may use
The
(.+)?
is changed to([^/]+)?
to make sure the first group value is only matching within the hostname part.I changed
(wistia.com|wi.st)
to(wistia.(?:com|net)|wi.st)
since you have such an example withwistia.net
to match.The
(?:/.*)?
part makes sure you match anything before an expected value that is captured into Group 4.