This is my setup:
var test =
"http://tv.website.com/video/8086844/randomstring/".match(/^.+tv.website.com\/video\/(.*\d)/);
I want to extract the video id(8086844) and the regex does work, however when another digit is added after the "randomstring", it also matches the 8086844 + randomstring + other number.
What do I have to change in order to always get just the video id.
Output
You are almost there. We know that, its going to be only numbers. So instead of
.*\d
we are gathering only the digits and grouping them using parens.This is the simplest of all:
Use substr here
Fiddle
To extract out the id from your string test:We use substr(from,to)
Try this regex
It will search every digit character after
...video\
word and then give all the concordant digits thereafter till any non-digit character comesThe problem is the
(.*\d)
part, it looks for a greedy string ends with a digit, instead you need a continues series of digits aftervideo/
, it can be done via(\d+)
change it to