简体   繁体   English

从html代码获取IP,但不是内部href标签中的一个

[英]Get an IP from the html code, BUT not the one inside href tag

ONLY I'm trying go get the list of IP's from the string: 我只想从字符串中获取IP的列表:

 <tr><td><a href="ip.php?ip=95.189.46.67">95.189.46.67</a></td><td>0</td></tr>
    <tr><td><a href="ip.php?ip=92.126.26.179">92.126.26.179</a></td><td>1</td></tr>

I use var ips= ThisString.match(/\\b\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}\\b/g).join("\\n"); 我使用var ips = ThisString.match(/ \\ b \\ d {1,3}。\\ d {1,3}。\\ d {1,3}。\\ d {1,3} \\ b / g).join ( “\\ n”);

Which gives me doubled list. 这给了我两倍的清单。 I need ips that are between >< like >95.189.46.67< .... Please... 我需要> <之间的ip,例如> 95.189.46.67 <...。请...

If the table has an identifier (ie class myTable ), you can iterate over each of the anchor elements in the table row using jQuery.each , and use jQuery.html to retrieve the contents: 如果表具有标识符(即myTable类),则可以使用jQuery.each遍历表行中的每个锚元素 ,然后使用jQuery.html检索内容:

$(".myTable tr td a").each(function(){
    console.log($(this).html());
})

jsfiddle 的jsfiddle

Put it into a table, then query out the created anchors' textContent properties 将其放入表中,然后查询出创建的锚点的textContent属性

var myString = '<tr><td><a href="ip.php?ip=95.189.46.67">95.189.46.67</a></td><td>0</td></tr>\
<tr><td><a href="ip.php?ip=92.126.26.179">92.126.26.179</a></td><td>1</td></tr>';
var table = document.createElement('table');
table.innerHTML = myString;
var ips = [].slice.call(table.querySelectorAll('a[href*="?ip="]')).map(anchor => anchor.textContent);

Or if you really want to use regex and only get the text inside the anchor (presuming your string will always be just like this example), you could use this, but the above will be safer. 或者,如果您真的想使用正则表达式并且只将文本放在锚点内(假定您的字符串始终像此示例一样),则可以使用它,但是上面的方法更安全。

var ips = myString.match(/\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}(?=<)/g);

Use RegExp.exec so you can use the "><" characters explicitly, and also use capture groups to grab just the IPs: 使用RegExp.exec以便可以显式使用“> <”字符,还可以使用捕获组仅捕获IP:

 var rx = />(\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3})</g var results; while(results = rx.exec(document.body.innerHTML) !== null){ console.log(results[1]); } 
 <table> <tr><td><a href="ip.php?ip=95.189.46.67">95.189.46.67</a></td><td>0</td></tr> <tr><td><a href="ip.php?ip=92.126.26.179">92.126.26.179</a></td><td>1</td></tr> </table> 

Results printed to the console: 结果打印到控制台:

95.189.46.67
92.126.26.179

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM