2

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/

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

2 Comments

You could try this instead return $(this).attr('href').replace(/\D/g,'');
@AlexLittlejohn that will fail for this url ---> http://someblog.it/blogpost123/7/-----.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.