Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Nodejs Regex: Extract text from between two strings

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?

like image 517
opticon Avatar asked Mar 27 '26 17:03

opticon


1 Answers

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"
like image 101
hwnd Avatar answered Mar 30 '26 10:03

hwnd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!