获取两个<a> tags?

时间:2015-07-14 01:02:47

标签: javascript regex

I need to take this:

<a href="mailto:email@email.com">email@email.com</a>

and have it return

email@email.com

And I'm not quite sure how to go about doing this? I'm really horrible with regex but I'm pretty sure I just need to find "> and get what comes between that and </a> but I'm not sure how to search for the thing between two other things.

edit: I actually found the regex I need: .replace(/(<([^>]+)>)/ig,'') to strip the tags away and give the inner contents, but the solutions down below are probably the better way to do it. Thanks guys!

5 个答案:

答案 0 :(得分:2)

如果html上有一个document.getElementsByTagName("a")[0].innerHTML;标记,则使用

<a>

如果有多个<a href="mailto:email@email.com" id="someID">email@email.com</a>代码,请尝试为其提供ID

document.getElementByID("someID").innerHTML;

然后在javascript中: -

var ffmpeg = require('ffmpeg');

答案 1 :(得分:1)

您可以使用:

&#13;
&#13;
var a = document.getElementsByTagName('a');
for (var i = 0;i<a.length;i++) {
  console.log(a[i].getAttribute('href').replace('mailto:',''))
}
&#13;
<a href="mailto:email1@email.com">email@email.com</a>
<a href="mailto:email2@email.com">email@email.com</a>
<a href="mailto:email3@email.com">email@email.com</a>
<a href="mailto:email4@email.com">email@email.com</a>
<a href="mailto:email5@email.com">email@email.com</a>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

如果您的A标记是DOM节点:

<a id="test" href="mailto:email@email.com">email@email.com</a>

<script>
console.log(document.getElementById('test').innerHTML);
</script>

如果这是服务器端的字符串:

$str='<a href="mailto:email@email.com">email@email.com</a>';
echo strip_tags($str);

答案 3 :(得分:1)

另一种可能性......

<强> HTML

<a href="mailto:email@email.com">email@email.com</a>
<a href="mailto:email@gmail.com">email@gmail.com</a>

<强> JAVASCRIPT

var a = document.getElementsByTagName('a');
for(var i = 0; i < a.length; i++){
   alert(a[i].getAttribute('href').split(':')[1]);
}

Check out this fiddle

答案 4 :(得分:0)

使用jquery非常简单:

$("a[href^='mailto']").html();

如果您有多个mailto链接:

$.each($("a[href^='mailto']"), function( index, element ) {
  console.log($(element).html());
});