11

I'm trying to extract the Vine ID from the following URL:

https://vine.co/v/Mipm1LMKVqJ/embed

I'm using this regex:

/v/(.*)/

and testing it here: http://regexpal.com/

...but it's matching the V and closing "/". How can I just get "Mipm1LMKVqJ", and what would be the cleanest way to do this in Node?

4
  • Although you should not use regex, it works fine and you need to reference group index 1 to get the match result. Commented Sep 22, 2014 at 1:15
  • As for your regex, you should make the * non greedy, so (.*?). And like hwnd said, grab the sub-group. Commented Sep 22, 2014 at 1:16
  • s = s.slice(s.indexOf("/v/") + 3); s.slice(0, s.indexOf("/")); I had my indices mixed up on the first version. Commented Sep 22, 2014 at 1:21
  • It is not a nodejs regex, there is no such thing. Commented Sep 22, 2014 at 1:55

1 Answer 1

17

You need to reference the first match group in order to print the match result only.

var re = new RegExp('/v/(.*)/');
var r  = 'https://vine.co/v/Mipm1LMKVqJ/embed'.match(re);
if (r)
    console.log(r[1]); //=> "Mipm1LMKVqJ"

Note: If the url often change, I recommend using *? to prevent greediness in your match.

Although from the following url, maybe consider splitting.

var r = 'https://vine.co/v/Mipm1LMKVqJ/embed'.split('/')[4]
console.log(r); //=> "Mipm1LMKVqJ"
Sign up to request clarification or add additional context in comments.

Comments

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.