paco paco - 4 months ago 10
jQuery Question

return all urls which contain string in javascript/jquery

I'm sure this is super easy, but I want to run a jquery/javascript script to return an array of all URLs on a page v which contains the '601' within it.

Answer

You can use the selector a[href*='601'] to get each element which href contains the text "601". For putting them in array you can use .each() and then push() the href attributes:

var arr = [];
$("a[href*='601']").each(function(){
    arr.push($(this).attr('href'));
})
console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="link" href="xxxx.com/6012342342/example"></a> 
<a class="link" href="xxxx.com/2342342/example"></a>
<a class="link" href="xxxx.com/2342342/example601"></a>
<a class="link" href="xxxx.com/6012342342/601example"></a>
<a class="link" href="xxxx.com/foo/example"></a>

Comments