Javascript JS Regex 查找几个 a 标签的 href

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13802334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:56:10  来源:igfitidea点击:

JS Regex to find href of several a tags

javascriptregex

提问by Infra Stank

I need a regex to find the contents of the hrefs from these a tags :

我需要一个正则表达式来从这些 a 标签中找到 hrefs 的内容:

<p class="bc_shirt_delete">
   <a href="/CustomContentProcess.aspx?CCID=13524&amp;OID=3936923&amp;A=Delete" onclick="javascript:return confirm('Are You sure you want to delete this item?')">delete</a>
</p>

Just the urls, not the href/ tags.

只是网址,而不是 href/ 标签。

I'm parsing a plain text ajax request here, so I need a regex.

我在这里解析纯文本ajax请求,所以我需要一个正则表达式。

回答by Niels

You can try this regex:

你可以试试这个正则表达式:

/href="([^\'\"]+)/g

Example at: http://regexr.com?333d1

示例在:http: //regexr.com?333d1

Update:or easier via non greedy method:

更新:或更容易通过非贪婪方法:

/href="(.*?)"/g

回答by gkiely

This will do it nicely. http://jsfiddle.net/grantk/cvBae/3/

这将做得很好。http://jsfiddle.net/grantk/cvBae/3/

var str = '<p href="missme" class="test"><a href="/CustomContentProcess.aspx?CCID=13524&amp;OID=3936923&amp;A=Delete" onclick="">delete</a></p>'
    
    var patt = /<a href="(.*?)"/g;
    while(match=patt.exec(str)){
     alert(match[1]);
    }

回答by SaidbakR

You may don't need Regex to do that.

您可能不需要正则表达式来做到这一点。

o = document.getElementsByTagName('a');
urls = Array();
for (i =0; i < o.length; i++){
   urls[i] = o[i].href;
}

If it is a plain text, you may insert it into a displayed non DOM element, i.e display: none, and then deal with it regularly in a way like I described.

如果是纯文本,您可以将其插入到显示的非 DOM 元素中,即display: none,然后按照我描述的方式定期处理它。

回答by jimasun

Here is a robust solution:

这是一个强大的解决方案:

let href_regex = /<a([^>]*?)href\s*=\s*(['"])([^]*?)*>/i,
    link_text = '<a href="/another-article/">another article link</a>',
    href = link_text.replace ( href_regex , '' );

Coloured href RegEx from http://www.regexr.com

来自 http://www.regexr.com 的彩色 href RegEx

What it does:

它能做什么:

  • detects a tags
  • lazy skips over other HTML attributes and groups (1) so you DRY
  • matches hrefattribute
  • takes in consideration possible whitespace around =
  • makes a group (2) of 'and "so you DRY
  • matches anything but group (1) and groups (3) it
  • matches the group (2) of 'and "
  • matches the group (1) (other attributes)
  • matches whatever else is there until closing the tag
  • set proper flags iignore case
  • 检测标签
  • 懒惰跳过其他 HTML 属性和组 (1) 所以你 DRY
  • 匹配href属性
  • 考虑周围可能的空白 =
  • 做一组(2)的'",所以你DRY
  • 匹配除组 (1) 和组 (3) 之外的任何内容
  • 匹配组 (2)'"
  • 匹配组 (1)(其他属性)
  • 匹配任何其他存在直到关闭标签
  • 设置适当的标志i忽略大小写

回答by jermel

It might be easier to use jQuery

使用 jQuery 可能更容易

 var html = '<li><h2 class="saved_shirt_name">new shirt 1</h2><button class="edit_shirt">Edit Shirt</button><button class="delete_shirt" data-eq="0" data-href="/CustomContentProcess.aspx?CCID=13524&amp;OID=3936923&amp;A=Delete">Delete Shirt</button></li><li><h2 class="saved_shirt_name">new shirt 2</h2><button class="edit_shirt">Edit Shirt</button><button class="delete_shirt" data-eq="0" data-href="/CustomContentProcess.aspx?CCID=13524&amp;OID=3936924&amp;A=Delete">Delete Shirt</button></li><li><h2 class="saved_shirt_name">new shirt 3</h2><button class="edit_shirt">Edit Shirt</button><button class="delete_shirt" data-eq="0" data-href="/CustomContentProcess.aspx?CCID=13524&amp;OID=3936925&amp;A=Delete">Delete Shirt</button></li>';
$(html).find('[data-href]');

And iterate each node

并迭代每个节点

UPDATE(because post updated)

更新(因为帖子更新了)

Let html be your raw response

让 html 成为您的原始响应

var matches = $(html).find('[href]');
var hrefs = [];
$.each(matches, function(i, el){ hrefs.push($(el).attr('href'));});
//hrefs is an array of matches

回答by EBFE

I combined a few solutions around and came up with this (Tested in .NET):

我结合了一些解决方案并提出了这个(在 .NET 中测试):

(?<=href=[\'\"])([^\'\"]+)

Explanation:

解释:

(?<=) : look behind so it wont include these characters

(?<=) : 往后看,所以它不会包含这些字符

[\'\"] : match both single and double quote

[\'\"] : 匹配单引号和双引号

[^] : match everything else except the characters after '^' in here

[^] : 匹配此处除 '^' 后的字符以外的所有内容

+ : one or more occurrence of last character.

+ :最后一个字符出现一次或多次。

This works well and is not greedy with the quote as it would stop matching the moment it finds a quote

这很有效,并且不会贪婪地引用引用,因为它会在找到引用的那一刻停止匹配

回答by Alex

how about spaces around = ? this code will fix it:

= 周围的空格怎么样?此代码将修复它:

var matches = str.match(/href( *)=( *)("|'*)(.*?)("|'*)( |>)/);
console.log(matches);

回答by andlrc

var str = "";

str += "<p class=\"bc_shirt_delete\">";
str += "<a href=\"/CustomContentProcess.aspx?CCID=13524&amp;OID=3936923&amp;A=Delete\" onclick=\"javascript:return confirm('Are You sure you want to delete this item?')\">delete</a>";
str += "</p>";


var matches = [];

str.replace(/href=("|')(.*?)("|')/g, function(a, b, match) {
  matches.push(match);
});

console.log(matches);

or if you don't care about the href:

或者如果你不关心href:

var matches = str.match(/href=("|')(.*?)("|')/);

console.log(matches);

回答by Frank Nocke

It's important to be non-greedy. And to cater for —matching— 'or"

重要的是不贪婪。并迎合——匹配——'"

test = "<a href="#" class="foo bar"> banana 
        <a href='http://google.de/foo?yes=1&no=2' data-href='foobar'/>"

test.replace(/href=(?:\'.*?\'|\".*?\")/gi,'');

disclaimer: The one thing it does not catch is html5 attribs data-href...

免责声明:它没有抓住的一件事是 html5 attribs data-href ...