5
I need a basic example(s) of how I can make a tiny script to pick up only the contents of(s) attribute href
.
I’ll simplify the explanation into two groups, it’s them: To and B
In the Group A we have the respective links started with Slash /
preceded by the word - mango. Ja para the Group B we have the same setindo, with detail that the word there associated with the code is khaki other than sleeve.
So stay like this:
Group A
<a href='/manga?v=1234567890'>A</a>
<a href='/manga?v=1234567890'>A</a>
<a href='/manga?v=1234567890'>A</a>
etc....
Group B
<a href='/caqui?v=1234567890'>B</a>
<a href='/caqui?v=1234567890'>B</a>
<a href='/caqui?v=1234567890'>B</a>
etc....
How to deal with one of these groups bringing forward on the page only the links referring to the code mango? The rest shall not appear.
Heed! - They all don’t have class
and neither id
, but have in its attribute identical values if compared to the same group belonging to the same segment. I just want mango.
So come on!
I have made the loop to go through the elements on the tag a
, it returns me all links existing on the page. See:
var link = document.getElementsByTagName('a');
for (var i = 0; i < link.length; i++) {
document.getElementById("resultado").innerHTML+= "<br>"+link[i].getAttribute('href')+"<br>"
}
What I ask here is the junction of link[i].getAttribute('href')
with a regular expression
function ExtrairID(url){
var regExp = /^.*((manga\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
if ( match && match[7].length == 10 ){
return match[7];
}else{
alert("Não foi possível extrair a ID.");
}
}
I have already made some attempts on my own, then I ended up getting lost so I decided to come here question. Without more, I wait answer or brief explanation.
The size is always the same ? 10 digits ?
– NoobSaibot
As much as it works with regex, the recommended thing to work with HTML is to use a DOM for Javascript (or other web-based automated). Often you may come across situations that a simple Regex will not be enough.
– danieltakeshi
@danieltakeshi I’m sorry but, I don’t understand what you wanted to pass me. Could you kindly be clearer on the subject?
– Diego Henrique
What do you mean "bringing forward the page"?
– Sam
You want to get all the
href
sleeved?– Sam
@DVD That’s what you got. =)
– Diego Henrique
What I wanted to explain, is that Regex is not the most recommended. And the dvd response is more recommended... with
.getAttribute("href");
– danieltakeshi