I am the worst guy on regular expression and any string operation in JavaScript/jquery, can you please help with some stuff ?
http://farm5.static.flickr.com/4040/5160997583_5ea677e4e1_s.jpg
this is an URL structur开发者_Go百科e example in need to get from this url 5160997583
Thanks for helping !
Assuming the structure is always the same and that you want to find all characters of the file name up to the first occurrence of an underscore, you can just do:
var str = "http://farm5.static.flickr.com/4040/5160997583_5ea677e4e1_s.jpg";
var num = str.split("/");
num = num[num.length - 1].split("_")[0];
alert(num);
str.split("/")[-1]
gets 5160997583_5ea677e4e1_s.jpg
and then we split that string on _
and grab the first part of that string.
Alternately, you could do:
var num = str.replace(/\/(\d+)_[^\/]+$/i, "$1");
var code = url.match(/\/([0-9]+)_[^\/]*$/)[1];
(EDITED to be more restrictive)
A regex that will find the starting digits of the file name would be this one
/\/(\d+)[^\/]+$/
and you use it like this
url = "http://farm5.static.flickr.com/4040/5160997583_5ea677e4e1_s.jpg";
matches = url.match(/\/(\d+)[^\/]+$/);
alert(matches[1]); // here it is /
精彩评论