views:

160

answers:

3

Hi,

I'm trying to create a gadget for win 7, that retrieves the RSS feed from a site. So far so good, everything works fine, just that I want to add something extra. The gadget so far extracts the link from the feed and stores it in a variable named 'articlelink', the link is usually something like "h*t*t*p://site.ro/film/2009/brxfcno-/22462" or "h*t*t*p://site.ro/serial/2004/veronica-mars---sez-3/1902".

This variable is used to create a link in the title of the flyout window that appears when the link in the gadget window is pressed.

What I need is a piece of code that extracts the number at the end (22462, 1902) and stores it in another variable so I can create a new link with it, which can be displayed in the flyout window as a separate link.

Example

initial link h*t*t*p://site.ro/serial/2004/veronica-mars---sezonul-3/1902

new link h*t*tp://site.ro/get/1902

Thank you, C.

+6  A: 
var link = "h*t*t*p://site.ro/serial/2004/veronica-mars---sezonul-3/1902";
var id = link.match(/\d+$/)[0]; // id will contain: 1902

Answering Splash's question below:

var matches = link.match(/([^/]+)\/(\d+)$/);
var id = matches[2]; // 1902
var title = matches[1]; // veronica-mars---sezonul-3
Infinity
It worked, thank you.
Splash
I am trying to figure out how to modify the piece of code from above so I can get this part now "veronica-mars---sezonul-3". Maybe someone can help with this.Regards,Chris.
Splash
I added the answer
Infinity
Thank you again.
Splash
+2  A: 

You can extract a substring, to get the characters between the last / and the end:

var id = link.substring(link.lastIndexOf('/') + 1); // 1902
CMS
+4  A: 

An idiom for getting the last part of a string:

 var id= link.split('/').pop();

Slightly more readable than than CMS's version, at the cost of being somewhat slower.

bobince