Question

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 "http://site.ro/film/2009/brxfcno-/22462" or "http://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 http://site.ro/serial/2004/veronica-mars---sezonul-3/1902

new link http://site.ro/get/1902

Was it helpful?

Solution

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

OTHER TIPS

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.

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

var id = link.substring(link.lastIndexOf('/') + 1); // 1902
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top