javascriptwindows-7windows-desktop-gadgets

Javascript code Substring


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


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