Question

I have the list of blogpost links on page

<ul class="postlist">
<li><a href="http://someblog.it/blogpost/7/-----.aspx">Post One</a></li>
<li><a href="http://someblog.it/blogpost/32/----------.aspx">Post Two</a></li>
<li><a href="http://someblog.it/blogpost/382/-----.aspx">Post Three</a></li>
<li><a href="http://someblog.it/blogpost/5782/-----------.aspx">Post Four</a></li>
<li><a href="http://someblog.it/blogpost/11682/-----------.aspx">Post Five</a></li>
</ul>

and want to get array of all numbers between slashes from this urls

$('a').each(function (index){
    var str = $(this).attr('href');
    var a = str.search(/[0-9]+/);
    var b = str.search(/-);
    console.log(str.substring(a,b));
});

smth like 7, 32, 382, 5782, 11682

http://jsfiddle.net/sYH56/

Was it helpful?

Solution

Like this -

var arr = $('a').map(function (index){
   var str = $(this).attr('href');
   var a = str.search(/[0-9]+/);
   var b = str.search(/\/-/);
   return str.substring(a,b);
}).get().join(',');

http://jsfiddle.net/sYH56/3/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top