简体   繁体   English

向所有外部链接添加 target="blank" 的正则表达式

[英]regular expression to add a target="blank" to all external links

Is it possible to add a target="_blank" to all <a> tags, with just one regular expression?是否可以仅使用一个正则表达式将target="_blank"添加到所有<a>标记? I've been experimenting with negative and positive look aheads/behinds, to no avail.我一直在尝试消极和积极的前瞻/后视,但无济于事。 This should:这应该:

  • Match anchor tags whose href starts with http://匹配hrefhttp://开头的锚标签
  • If the anchor tag does not have a target tag, adds a target="_blank" tag如果锚标签没有target标签,则添加一个target="_blank"标签
  • If it does have a target tag, it checks if the target tag is not already set to "_blank" , and if not it is replaced to target="_blank"如果它确实有target标签,它会检查target标签是否尚未设置为"_blank" ,如果没有,则将其替换为target="_blank"

Is this possible?这可能吗? If not, what would be the least computationally intensive way to do this?如果不是,那么计算密集度最低的方法是什么?

You didn't really specify whether this would be the html from the DOM (among other things). 你没有真正指定这是否是来自DOM的html(以及其他内容)。 Assuming you want to modify the DOM... Using jQuery, you could do something like this: 假设您要修改DOM ...使用jQuery,您可以执行以下操作:

$(document).ready( function() {
    $('a').filter( function() {
        return $(this).attr('href').substr(0, 7) == "http://";
    }).attr('target', '_blank');
});

here is a jsfiddle showing it works (you can inspect the elements to see that the a 's with href 's starting with http:// have target="_blank" ) : http://jsfiddle.net/amRrj/ 这是一个jsfiddle显示它的工作原理(你可以检查元素,看看ahref是以http://开头的,具有target="_blank" ): http//jsfiddle.net/amRrj/

If the HTML is also well-formed XML then you would be better off using an XML tool with support for xpath and xslt. 如果HTML也是格式良好的XML,那么最好使用支持xpath和xslt的XML工具。 Take a look at XMLStarlet which provides tools similar to grep, sed etc for working with XML. 看一下XMLStarlet ,它提供类似于grep,sed等工具来处理XML。 I think this would do what you want, but I have not tried it: 我认为这会做你想要的,但我没有尝试过:

xml ed -P -S -i //a -t attr -n target -v _blank somefile.html >somefile_2.html

xml ed invokes the XMLStarlet edit command xml ed调用XMLStarlet编辑命令

-P preserve formatting -P保留格式

-S preserve whitespace -S保留空白

-i //a insert at every tag -i //a每个标签-i //a插入

-t attr insert type is attribute -t attr insert type是属性

-n target name of attribute to insert -n target要插入的属性的-n target名称

-v _blank value of attribute to insert -v _blank要插入的属性值

For more complex editing you can use XMLStarlet with an xslt transform. 对于更复杂的编辑,您可以将XMLStarlet与xslt转换一起使用。

Another option is adding target="_blank" to the <base href…> . 另一个选择是将target="_blank"<base href…> This will globally affect the pages <a> tag targets rather than needing to add them individually. 这将全局影响页面<a>标记目标,而不是需要单独添加它们。 Target declarations on <a> tags will override this. <a>标签上的目标声明将覆盖此。

    var target = '_self';

    if(external){
        target = '_blank';
    }

    $('#base_tag').attr('target', target);

Fiddle 小提琴

<base> Documentation <base>文档

I write this one maybe it helps:我写这个也许有帮助:

Regex: /<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi正则表达式:/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/ /<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi

 console.log(`<a href="https://www.stackoverflow.com/test" target="_parent">Stackoverflow</a>`.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, '<a href="$2" target="_blank">$4</a>')); console.log(`<a href="https://www.stackoverflow.com/test" target="_target">Stackoverflow</a>`.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, '<a href="$2" target="_blank">$4</a>')); console.log(`<a href="https://www.stackoverflow.com/test">Stackoverflow</a>`.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, '<a href="$2" target="_blank">$4</a>'));

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

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