开发者

Javascript code Substring

开发者 https://www.devze.com 2022-12-09 18:16 出处:网络
I\'m trying to create a gadget for win 7, that retrieves the RSS feed from a site. S开发者_运维问答o far so good, everything works fine, just that I want to add something extra. The gadget so far extr

I'm trying to create a gadget for win 7, that retrieves the RSS feed from a site. S开发者_运维问答o 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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号