2

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Splash
  • 161
  • 3
  • 11

3 Answers3

6
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
adamJLev
  • 13,713
  • 11
  • 60
  • 65
  • 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 Nov 02 '09 at 08:03
4

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
  • 528,062
  • 107
  • 651
  • 834
2

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

var id = link.substring(link.lastIndexOf('/') + 1); // 1902
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838